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 | |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 26 | import org.xmlpull.v1.XmlPullParserException; |
| 27 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 28 | import android.content.ComponentName; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.ServiceConnection; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.ResolveInfo; |
| 34 | import android.content.pm.ServiceInfo; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 35 | import android.os.Binder; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 36 | import android.os.Bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 37 | import android.os.IBinder; |
| 38 | import android.os.RemoteException; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 39 | import android.provider.Settings; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 40 | import android.service.textservice.SpellCheckerService; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 41 | import android.text.TextUtils; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 42 | import android.util.Slog; |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 43 | import android.view.inputmethod.InputMethodManager; |
| 44 | import android.view.inputmethod.InputMethodSubtype; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 45 | import android.view.textservice.SpellCheckerInfo; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 46 | import android.view.textservice.SpellCheckerSubtype; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 47 | |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 48 | import java.io.FileDescriptor; |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 49 | import java.io.IOException; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 50 | import java.io.PrintWriter; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 51 | import java.util.ArrayList; |
| 52 | import java.util.HashMap; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 53 | import java.util.List; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 54 | import java.util.Map; |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 55 | import java.util.concurrent.CopyOnWriteArrayList; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 56 | |
| 57 | public class TextServicesManagerService extends ITextServicesManager.Stub { |
| 58 | private static final String TAG = TextServicesManagerService.class.getSimpleName(); |
| 59 | private static final boolean DBG = false; |
| 60 | |
| 61 | private final Context mContext; |
| 62 | private boolean mSystemReady; |
| 63 | private final TextServicesMonitor mMonitor; |
| 64 | private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap = |
| 65 | new HashMap<String, SpellCheckerInfo>(); |
| 66 | private final ArrayList<SpellCheckerInfo> mSpellCheckerList = new ArrayList<SpellCheckerInfo>(); |
| 67 | private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups = |
| 68 | new HashMap<String, SpellCheckerBindGroup>(); |
| 69 | |
| 70 | public void systemReady() { |
| 71 | if (!mSystemReady) { |
| 72 | mSystemReady = true; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public TextServicesManagerService(Context context) { |
| 77 | mSystemReady = false; |
| 78 | mContext = context; |
| 79 | mMonitor = new TextServicesMonitor(); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 80 | mMonitor.register(context, null, true); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 81 | synchronized (mSpellCheckerMap) { |
| 82 | buildSpellCheckerMapLocked(context, mSpellCheckerList, mSpellCheckerMap); |
| 83 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 84 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
| 85 | if (sci == null) { |
| 86 | sci = findAvailSpellCheckerLocked(null, null); |
| 87 | if (sci != null) { |
| 88 | // Set the current spell checker if there is one or more spell checkers |
| 89 | // available. In this case, "sci" is the first one in the available spell |
| 90 | // checkers. |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 91 | setCurrentSpellCheckerLocked(sci.getId()); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 92 | } |
| 93 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private class TextServicesMonitor extends PackageMonitor { |
| 97 | @Override |
| 98 | public void onSomePackagesChanged() { |
| 99 | synchronized (mSpellCheckerMap) { |
| 100 | buildSpellCheckerMapLocked(mContext, mSpellCheckerList, mSpellCheckerMap); |
| 101 | // TODO: Update for each locale |
| 102 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 103 | if (sci == null) return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 104 | final String packageName = sci.getPackageName(); |
| 105 | final int change = isPackageDisappearing(packageName); |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 106 | if (// Package disappearing |
| 107 | change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE |
| 108 | // Package modified |
| 109 | || isPackageModified(packageName)) { |
| 110 | sci = findAvailSpellCheckerLocked(null, packageName); |
| 111 | if (sci != null) { |
| 112 | setCurrentSpellCheckerLocked(sci.getId()); |
| 113 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private static void buildSpellCheckerMapLocked(Context context, |
| 120 | ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map) { |
| 121 | list.clear(); |
| 122 | map.clear(); |
| 123 | final PackageManager pm = context.getPackageManager(); |
| 124 | List<ResolveInfo> services = pm.queryIntentServices( |
| 125 | new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA); |
| 126 | final int N = services.size(); |
| 127 | for (int i = 0; i < N; ++i) { |
| 128 | final ResolveInfo ri = services.get(i); |
| 129 | final ServiceInfo si = ri.serviceInfo; |
| 130 | final ComponentName compName = new ComponentName(si.packageName, si.name); |
| 131 | if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) { |
| 132 | Slog.w(TAG, "Skipping text service " + compName |
| 133 | + ": it does not require the permission " |
| 134 | + android.Manifest.permission.BIND_TEXT_SERVICE); |
| 135 | continue; |
| 136 | } |
| 137 | if (DBG) Slog.d(TAG, "Add: " + compName); |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 138 | try { |
| 139 | final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri); |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 140 | if (sci.getSubtypeCount() <= 0) { |
| 141 | Slog.w(TAG, "Skipping text service " + compName |
| 142 | + ": it does not contain subtypes."); |
| 143 | continue; |
| 144 | } |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 145 | list.add(sci); |
| 146 | map.put(sci.getId(), sci); |
| 147 | } catch (XmlPullParserException e) { |
| 148 | Slog.w(TAG, "Unable to load the spell checker " + compName, e); |
| 149 | } catch (IOException e) { |
| 150 | Slog.w(TAG, "Unable to load the spell checker " + compName, e); |
| 151 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 152 | } |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 153 | if (DBG) { |
| 154 | Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size()); |
| 155 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // TODO: find an appropriate spell checker for specified locale |
| 159 | private SpellCheckerInfo findAvailSpellCheckerLocked(String locale, String prefPackage) { |
| 160 | final int spellCheckersCount = mSpellCheckerList.size(); |
| 161 | if (spellCheckersCount == 0) { |
| 162 | Slog.w(TAG, "no available spell checker services found"); |
| 163 | return null; |
| 164 | } |
| 165 | if (prefPackage != null) { |
| 166 | for (int i = 0; i < spellCheckersCount; ++i) { |
| 167 | final SpellCheckerInfo sci = mSpellCheckerList.get(i); |
| 168 | if (prefPackage.equals(sci.getPackageName())) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 169 | if (DBG) { |
| 170 | Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName()); |
| 171 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 172 | return sci; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | if (spellCheckersCount > 1) { |
| 177 | Slog.w(TAG, "more than one spell checker service found, picking first"); |
| 178 | } |
| 179 | return mSpellCheckerList.get(0); |
| 180 | } |
| 181 | |
| 182 | // TODO: Save SpellCheckerService by supported languages. Currently only one spell |
| 183 | // checker is saved. |
| 184 | @Override |
| 185 | public SpellCheckerInfo getCurrentSpellChecker(String locale) { |
| 186 | synchronized (mSpellCheckerMap) { |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 187 | final String curSpellCheckerId = |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 188 | Settings.Secure.getString(mContext.getContentResolver(), |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 189 | Settings.Secure.SELECTED_SPELL_CHECKER); |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 190 | if (DBG) { |
| 191 | Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId); |
| 192 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 193 | if (TextUtils.isEmpty(curSpellCheckerId)) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 194 | return null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 195 | } |
| 196 | return mSpellCheckerMap.get(curSpellCheckerId); |
| 197 | } |
| 198 | } |
| 199 | |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 200 | // TODO: Respect allowImplicitlySelectedSubtype |
Satoshi Kataoka | 17150cf | 2012-05-30 20:05:44 +0900 | [diff] [blame] | 201 | // TODO: Save SpellCheckerSubtype by supported languages by looking at "locale". |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 202 | @Override |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 203 | public SpellCheckerSubtype getCurrentSpellCheckerSubtype( |
| 204 | String locale, boolean allowImplicitlySelectedSubtype) { |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 205 | synchronized (mSpellCheckerMap) { |
| 206 | final String subtypeHashCodeStr = |
| 207 | Settings.Secure.getString(mContext.getContentResolver(), |
| 208 | Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE); |
| 209 | if (DBG) { |
satok | c7b60f72 | 2011-08-31 16:30:27 +0900 | [diff] [blame] | 210 | Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCodeStr); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 211 | } |
| 212 | final SpellCheckerInfo sci = getCurrentSpellChecker(null); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 213 | if (sci == null || sci.getSubtypeCount() == 0) { |
| 214 | if (DBG) { |
| 215 | Slog.w(TAG, "Subtype not found."); |
| 216 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 217 | return null; |
| 218 | } |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 219 | final int hashCode; |
| 220 | if (!TextUtils.isEmpty(subtypeHashCodeStr)) { |
| 221 | hashCode = Integer.valueOf(subtypeHashCodeStr); |
| 222 | } else { |
| 223 | hashCode = 0; |
| 224 | } |
| 225 | if (hashCode == 0 && !allowImplicitlySelectedSubtype) { |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 226 | return null; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 227 | } |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 228 | String candidateLocale = null; |
| 229 | if (hashCode == 0) { |
| 230 | // Spell checker language settings == "auto" |
| 231 | final InputMethodManager imm = |
| 232 | (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 233 | if (imm != null) { |
| 234 | final InputMethodSubtype currentInputMethodSubtype = |
| 235 | imm.getCurrentInputMethodSubtype(); |
| 236 | if (currentInputMethodSubtype != null) { |
| 237 | final String localeString = currentInputMethodSubtype.getLocale(); |
| 238 | if (!TextUtils.isEmpty(localeString)) { |
| 239 | // 1. Use keyboard locale if available in the spell checker |
| 240 | candidateLocale = localeString; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | if (candidateLocale == null) { |
| 245 | // 2. Use System locale if available in the spell checker |
| 246 | candidateLocale = mContext.getResources().getConfiguration().locale.toString(); |
| 247 | } |
| 248 | } |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 249 | SpellCheckerSubtype candidate = null; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 250 | for (int i = 0; i < sci.getSubtypeCount(); ++i) { |
| 251 | final SpellCheckerSubtype scs = sci.getSubtypeAt(i); |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 252 | if (hashCode == 0) { |
Satoshi Kataoka | 17150cf | 2012-05-30 20:05:44 +0900 | [diff] [blame] | 253 | final String scsLocale = scs.getLocale(); |
| 254 | if (candidateLocale.equals(scsLocale)) { |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 255 | return scs; |
| 256 | } else if (candidate == null) { |
satok | 7018a90 | 2012-05-24 18:10:37 +0900 | [diff] [blame] | 257 | if (candidateLocale.length() >= 2 && scsLocale.length() >= 2 |
| 258 | && candidateLocale.startsWith(scsLocale)) { |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 259 | // Fall back to the applicable language |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 260 | candidate = scs; |
| 261 | } |
| 262 | } |
| 263 | } else if (scs.hashCode() == hashCode) { |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 264 | if (DBG) { |
satok | 70deff4 | 2011-09-30 15:14:21 +0900 | [diff] [blame] | 265 | Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale |
| 266 | + ", " + scs.getLocale()); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 267 | } |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 268 | // 3. Use the user specified spell check language |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 269 | return scs; |
| 270 | } |
| 271 | } |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 272 | // 4. Fall back to the applicable language and return it if not null |
| 273 | // 5. Simply just return it even if it's null which means we could find no suitable |
| 274 | // spell check languages |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 275 | return candidate; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 279 | @Override |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 280 | public void getSpellCheckerService(String sciId, String locale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 281 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener, |
| 282 | Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 283 | if (!mSystemReady) { |
| 284 | return; |
| 285 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 286 | if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 287 | Slog.e(TAG, "getSpellCheckerService: Invalid input."); |
| 288 | return; |
| 289 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 290 | synchronized(mSpellCheckerMap) { |
| 291 | if (!mSpellCheckerMap.containsKey(sciId)) { |
| 292 | return; |
| 293 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 294 | final SpellCheckerInfo sci = mSpellCheckerMap.get(sciId); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 295 | final int uid = Binder.getCallingUid(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 296 | if (mSpellCheckerBindGroups.containsKey(sciId)) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 297 | final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId); |
| 298 | if (bindGroup != null) { |
| 299 | final InternalDeathRecipient recipient = |
| 300 | mSpellCheckerBindGroups.get(sciId).addListener( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 301 | tsListener, locale, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 302 | if (recipient == null) { |
| 303 | if (DBG) { |
| 304 | Slog.w(TAG, "Didn't create a death recipient."); |
| 305 | } |
| 306 | return; |
| 307 | } |
| 308 | if (bindGroup.mSpellChecker == null & bindGroup.mConnected) { |
| 309 | Slog.e(TAG, "The state of the spell checker bind group is illegal."); |
| 310 | bindGroup.removeAll(); |
| 311 | } else if (bindGroup.mSpellChecker != null) { |
| 312 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 313 | Slog.w(TAG, "Existing bind found. Return a spell checker session now. " |
| 314 | + "Listeners count = " + bindGroup.mListeners.size()); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 315 | } |
| 316 | try { |
| 317 | final ISpellCheckerSession session = |
| 318 | bindGroup.mSpellChecker.getISpellCheckerSession( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 319 | recipient.mScLocale, recipient.mScListener, bundle); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 320 | if (session != null) { |
| 321 | tsListener.onServiceConnected(session); |
| 322 | return; |
| 323 | } else { |
| 324 | if (DBG) { |
| 325 | Slog.w(TAG, "Existing bind already expired. "); |
| 326 | } |
| 327 | bindGroup.removeAll(); |
| 328 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 329 | } catch (RemoteException e) { |
| 330 | Slog.e(TAG, "Exception in getting spell checker session: " + e); |
| 331 | bindGroup.removeAll(); |
| 332 | } |
| 333 | } |
| 334 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 335 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 336 | final long ident = Binder.clearCallingIdentity(); |
| 337 | try { |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 338 | startSpellCheckerServiceInnerLocked( |
| 339 | sci, locale, tsListener, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 340 | } finally { |
| 341 | Binder.restoreCallingIdentity(ident); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 342 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 343 | } |
| 344 | return; |
| 345 | } |
| 346 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 347 | @Override |
| 348 | public boolean isSpellCheckerEnabled() { |
| 349 | synchronized(mSpellCheckerMap) { |
| 350 | return isSpellCheckerEnabledLocked(); |
| 351 | } |
| 352 | } |
| 353 | |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 354 | private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale, |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 355 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 356 | int uid, Bundle bundle) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 357 | if (DBG) { |
| 358 | Slog.w(TAG, "Start spell checker session inner locked."); |
| 359 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 360 | final String sciId = info.getId(); |
| 361 | final InternalServiceConnection connection = new InternalServiceConnection( |
satok | 060677f | 2011-11-17 09:40:56 +0900 | [diff] [blame] | 362 | sciId, locale, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 363 | final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE); |
| 364 | serviceIntent.setComponent(info.getComponent()); |
| 365 | if (DBG) { |
| 366 | Slog.w(TAG, "bind service: " + info.getId()); |
| 367 | } |
| 368 | if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 369 | Slog.e(TAG, "Failed to get a spell checker service."); |
| 370 | return; |
| 371 | } |
| 372 | final SpellCheckerBindGroup group = new SpellCheckerBindGroup( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 373 | connection, tsListener, locale, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 374 | mSpellCheckerBindGroups.put(sciId, group); |
| 375 | } |
| 376 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 377 | @Override |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 378 | public SpellCheckerInfo[] getEnabledSpellCheckers() { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 379 | if (DBG) { |
| 380 | Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size()); |
| 381 | for (int i = 0; i < mSpellCheckerList.size(); ++i) { |
| 382 | Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName()); |
| 383 | } |
| 384 | } |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 385 | return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]); |
| 386 | } |
| 387 | |
| 388 | @Override |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 389 | public void finishSpellCheckerService(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 390 | if (DBG) { |
| 391 | Slog.d(TAG, "FinishSpellCheckerService"); |
| 392 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 393 | synchronized(mSpellCheckerMap) { |
satok | 4c3fa64 | 2011-11-30 18:17:59 +0900 | [diff] [blame] | 394 | final ArrayList<SpellCheckerBindGroup> removeList = |
| 395 | new ArrayList<SpellCheckerBindGroup>(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 396 | for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) { |
| 397 | if (group == null) continue; |
satok | 4c3fa64 | 2011-11-30 18:17:59 +0900 | [diff] [blame] | 398 | // Use removeList to avoid modifying mSpellCheckerBindGroups in this loop. |
| 399 | removeList.add(group); |
| 400 | } |
| 401 | final int removeSize = removeList.size(); |
| 402 | for (int i = 0; i < removeSize; ++i) { |
| 403 | removeList.get(i).removeListener(listener); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 408 | @Override |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 409 | public void setCurrentSpellChecker(String locale, String sciId) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 410 | synchronized(mSpellCheckerMap) { |
| 411 | if (mContext.checkCallingOrSelfPermission( |
| 412 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 413 | != PackageManager.PERMISSION_GRANTED) { |
| 414 | throw new SecurityException( |
| 415 | "Requires permission " |
| 416 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 417 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 418 | setCurrentSpellCheckerLocked(sciId); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 422 | @Override |
| 423 | public void setCurrentSpellCheckerSubtype(String locale, int hashCode) { |
| 424 | synchronized(mSpellCheckerMap) { |
| 425 | if (mContext.checkCallingOrSelfPermission( |
| 426 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 427 | != PackageManager.PERMISSION_GRANTED) { |
| 428 | throw new SecurityException( |
| 429 | "Requires permission " |
| 430 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 431 | } |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 432 | setCurrentSpellCheckerSubtypeLocked(hashCode); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public void setSpellCheckerEnabled(boolean enabled) { |
| 438 | synchronized(mSpellCheckerMap) { |
| 439 | if (mContext.checkCallingOrSelfPermission( |
| 440 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 441 | != PackageManager.PERMISSION_GRANTED) { |
| 442 | throw new SecurityException( |
| 443 | "Requires permission " |
| 444 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 445 | } |
| 446 | setSpellCheckerEnabledLocked(enabled); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 450 | private void setCurrentSpellCheckerLocked(String sciId) { |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 451 | if (DBG) { |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 452 | Slog.w(TAG, "setCurrentSpellChecker: " + sciId); |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 453 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 454 | if (TextUtils.isEmpty(sciId) || !mSpellCheckerMap.containsKey(sciId)) return; |
satok | f39daef | 2011-08-26 19:54:27 +0900 | [diff] [blame] | 455 | final SpellCheckerInfo currentSci = getCurrentSpellChecker(null); |
| 456 | if (currentSci != null && currentSci.getId().equals(sciId)) { |
| 457 | // Do nothing if the current spell checker is same as new spell checker. |
| 458 | return; |
| 459 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 460 | final long ident = Binder.clearCallingIdentity(); |
| 461 | try { |
| 462 | Settings.Secure.putString(mContext.getContentResolver(), |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 463 | Settings.Secure.SELECTED_SPELL_CHECKER, sciId); |
satok | f39daef | 2011-08-26 19:54:27 +0900 | [diff] [blame] | 464 | setCurrentSpellCheckerSubtypeLocked(0); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 465 | } finally { |
| 466 | Binder.restoreCallingIdentity(ident); |
| 467 | } |
| 468 | } |
| 469 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 470 | private void setCurrentSpellCheckerSubtypeLocked(int hashCode) { |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 471 | if (DBG) { |
| 472 | Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode); |
| 473 | } |
| 474 | final SpellCheckerInfo sci = getCurrentSpellChecker(null); |
satok | fbedf1a | 2011-08-26 15:48:50 +0900 | [diff] [blame] | 475 | int tempHashCode = 0; |
| 476 | for (int i = 0; sci != null && i < sci.getSubtypeCount(); ++i) { |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 477 | if(sci.getSubtypeAt(i).hashCode() == hashCode) { |
satok | fbedf1a | 2011-08-26 15:48:50 +0900 | [diff] [blame] | 478 | tempHashCode = hashCode; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 479 | break; |
| 480 | } |
| 481 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 482 | final long ident = Binder.clearCallingIdentity(); |
| 483 | try { |
| 484 | Settings.Secure.putString(mContext.getContentResolver(), |
satok | fbedf1a | 2011-08-26 15:48:50 +0900 | [diff] [blame] | 485 | Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(tempHashCode)); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 486 | } finally { |
| 487 | Binder.restoreCallingIdentity(ident); |
| 488 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 489 | } |
| 490 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 491 | private void setSpellCheckerEnabledLocked(boolean enabled) { |
| 492 | if (DBG) { |
| 493 | Slog.w(TAG, "setSpellCheckerEnabled: " + enabled); |
| 494 | } |
| 495 | final long ident = Binder.clearCallingIdentity(); |
| 496 | try { |
| 497 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 498 | Settings.Secure.SPELL_CHECKER_ENABLED, enabled ? 1 : 0); |
| 499 | } finally { |
| 500 | Binder.restoreCallingIdentity(ident); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | private boolean isSpellCheckerEnabledLocked() { |
| 505 | final long ident = Binder.clearCallingIdentity(); |
| 506 | try { |
| 507 | final boolean retval = Settings.Secure.getInt(mContext.getContentResolver(), |
| 508 | Settings.Secure.SPELL_CHECKER_ENABLED, 1) == 1; |
| 509 | if (DBG) { |
| 510 | Slog.w(TAG, "getSpellCheckerEnabled: " + retval); |
| 511 | } |
| 512 | return retval; |
| 513 | } finally { |
| 514 | Binder.restoreCallingIdentity(ident); |
| 515 | } |
| 516 | } |
| 517 | |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 518 | @Override |
| 519 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 520 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 521 | != PackageManager.PERMISSION_GRANTED) { |
| 522 | |
| 523 | pw.println("Permission Denial: can't dump TextServicesManagerService from from pid=" |
| 524 | + Binder.getCallingPid() |
| 525 | + ", uid=" + Binder.getCallingUid()); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | synchronized(mSpellCheckerMap) { |
| 530 | pw.println("Current Text Services Manager state:"); |
| 531 | pw.println(" Spell Checker Map:"); |
| 532 | for (Map.Entry<String, SpellCheckerInfo> ent : mSpellCheckerMap.entrySet()) { |
| 533 | pw.print(" "); pw.print(ent.getKey()); pw.println(":"); |
| 534 | SpellCheckerInfo info = ent.getValue(); |
| 535 | pw.print(" "); pw.print("id="); pw.println(info.getId()); |
| 536 | pw.print(" "); pw.print("comp="); |
| 537 | pw.println(info.getComponent().toShortString()); |
| 538 | int NS = info.getSubtypeCount(); |
| 539 | for (int i=0; i<NS; i++) { |
| 540 | SpellCheckerSubtype st = info.getSubtypeAt(i); |
| 541 | pw.print(" "); pw.print("Subtype #"); pw.print(i); pw.println(":"); |
| 542 | pw.print(" "); pw.print("locale="); pw.println(st.getLocale()); |
| 543 | pw.print(" "); pw.print("extraValue="); |
| 544 | pw.println(st.getExtraValue()); |
| 545 | } |
| 546 | } |
| 547 | pw.println(""); |
| 548 | pw.println(" Spell Checker Bind Groups:"); |
| 549 | for (Map.Entry<String, SpellCheckerBindGroup> ent |
| 550 | : mSpellCheckerBindGroups.entrySet()) { |
| 551 | SpellCheckerBindGroup grp = ent.getValue(); |
| 552 | pw.print(" "); pw.print(ent.getKey()); pw.print(" "); |
| 553 | pw.print(grp); pw.println(":"); |
| 554 | pw.print(" "); pw.print("mInternalConnection="); |
| 555 | pw.println(grp.mInternalConnection); |
| 556 | pw.print(" "); pw.print("mSpellChecker="); |
| 557 | pw.println(grp.mSpellChecker); |
| 558 | pw.print(" "); pw.print("mBound="); pw.print(grp.mBound); |
| 559 | pw.print(" mConnected="); pw.println(grp.mConnected); |
| 560 | int NL = grp.mListeners.size(); |
| 561 | for (int i=0; i<NL; i++) { |
| 562 | InternalDeathRecipient listener = grp.mListeners.get(i); |
| 563 | pw.print(" "); pw.print("Listener #"); pw.print(i); pw.println(":"); |
| 564 | pw.print(" "); pw.print("mTsListener="); |
| 565 | pw.println(listener.mTsListener); |
| 566 | pw.print(" "); pw.print("mScListener="); |
| 567 | pw.println(listener.mScListener); |
| 568 | pw.print(" "); pw.print("mGroup="); |
| 569 | pw.println(listener.mGroup); |
| 570 | pw.print(" "); pw.print("mScLocale="); |
| 571 | pw.print(listener.mScLocale); |
| 572 | pw.print(" mUid="); pw.println(listener.mUid); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 578 | // SpellCheckerBindGroup contains active text service session listeners. |
| 579 | // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from |
| 580 | // mSpellCheckerBindGroups |
| 581 | private class SpellCheckerBindGroup { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 582 | private final String TAG = SpellCheckerBindGroup.class.getSimpleName(); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 583 | private final InternalServiceConnection mInternalConnection; |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 584 | private final CopyOnWriteArrayList<InternalDeathRecipient> mListeners = |
| 585 | new CopyOnWriteArrayList<InternalDeathRecipient>(); |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 586 | public boolean mBound; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 587 | public ISpellCheckerService mSpellChecker; |
| 588 | public boolean mConnected; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 589 | |
| 590 | public SpellCheckerBindGroup(InternalServiceConnection connection, |
| 591 | ITextServicesSessionListener listener, String locale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 592 | ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 593 | mInternalConnection = connection; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 594 | mBound = true; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 595 | mConnected = false; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 596 | addListener(listener, locale, scListener, uid, bundle); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | public void onServiceConnected(ISpellCheckerService spellChecker) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 600 | if (DBG) { |
| 601 | Slog.d(TAG, "onServiceConnected"); |
| 602 | } |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 603 | |
| 604 | for (InternalDeathRecipient listener : mListeners) { |
| 605 | try { |
| 606 | final ISpellCheckerSession session = spellChecker.getISpellCheckerSession( |
| 607 | listener.mScLocale, listener.mScListener, listener.mBundle); |
| 608 | synchronized(mSpellCheckerMap) { |
| 609 | if (mListeners.contains(listener)) { |
| 610 | listener.mTsListener.onServiceConnected(session); |
| 611 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 612 | } |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 613 | } catch (RemoteException e) { |
| 614 | Slog.e(TAG, "Exception in getting the spell checker session." |
| 615 | + "Reconnect to the spellchecker. ", e); |
| 616 | removeAll(); |
| 617 | return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 618 | } |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 619 | } |
| 620 | synchronized(mSpellCheckerMap) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 621 | mSpellChecker = spellChecker; |
| 622 | mConnected = true; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 626 | public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 627 | String locale, ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 628 | if (DBG) { |
| 629 | Slog.d(TAG, "addListener: " + locale); |
| 630 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 631 | InternalDeathRecipient recipient = null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 632 | synchronized(mSpellCheckerMap) { |
| 633 | try { |
| 634 | final int size = mListeners.size(); |
| 635 | for (int i = 0; i < size; ++i) { |
| 636 | if (mListeners.get(i).hasSpellCheckerListener(scListener)) { |
| 637 | // do not add the lister if the group already contains this. |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 638 | return null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 639 | } |
| 640 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 641 | recipient = new InternalDeathRecipient( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 642 | this, tsListener, locale, scListener, uid, bundle); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 643 | scListener.asBinder().linkToDeath(recipient, 0); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 644 | mListeners.add(recipient); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 645 | } catch(RemoteException e) { |
| 646 | // do nothing |
| 647 | } |
| 648 | cleanLocked(); |
| 649 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 650 | return recipient; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | public void removeListener(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 654 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 655 | Slog.w(TAG, "remove listener: " + listener.hashCode()); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 656 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 657 | synchronized(mSpellCheckerMap) { |
| 658 | final int size = mListeners.size(); |
| 659 | final ArrayList<InternalDeathRecipient> removeList = |
| 660 | new ArrayList<InternalDeathRecipient>(); |
| 661 | for (int i = 0; i < size; ++i) { |
| 662 | final InternalDeathRecipient tempRecipient = mListeners.get(i); |
| 663 | if(tempRecipient.hasSpellCheckerListener(listener)) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 664 | if (DBG) { |
| 665 | Slog.w(TAG, "found existing listener."); |
| 666 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 667 | removeList.add(tempRecipient); |
| 668 | } |
| 669 | } |
| 670 | final int removeSize = removeList.size(); |
| 671 | for (int i = 0; i < removeSize; ++i) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 672 | if (DBG) { |
| 673 | Slog.w(TAG, "Remove " + removeList.get(i)); |
| 674 | } |
satok | 2520ed8 | 2011-10-31 19:38:05 +0900 | [diff] [blame] | 675 | final InternalDeathRecipient idr = removeList.get(i); |
| 676 | idr.mScListener.asBinder().unlinkToDeath(idr, 0); |
| 677 | mListeners.remove(idr); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 678 | } |
| 679 | cleanLocked(); |
| 680 | } |
| 681 | } |
| 682 | |
satok | 4c3fa64 | 2011-11-30 18:17:59 +0900 | [diff] [blame] | 683 | // cleanLocked may remove elements from mSpellCheckerBindGroups |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 684 | private void cleanLocked() { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 685 | if (DBG) { |
| 686 | Slog.d(TAG, "cleanLocked"); |
| 687 | } |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 688 | // If there are no more active listeners, clean up. Only do this |
| 689 | // once. |
| 690 | if (mBound && mListeners.isEmpty()) { |
| 691 | mBound = false; |
satok | c7b60f72 | 2011-08-31 16:30:27 +0900 | [diff] [blame] | 692 | final String sciId = mInternalConnection.mSciId; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 693 | SpellCheckerBindGroup cur = mSpellCheckerBindGroups.get(sciId); |
| 694 | if (cur == this) { |
satok | c7b60f72 | 2011-08-31 16:30:27 +0900 | [diff] [blame] | 695 | if (DBG) { |
| 696 | Slog.d(TAG, "Remove bind group."); |
| 697 | } |
| 698 | mSpellCheckerBindGroups.remove(sciId); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 699 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 700 | mContext.unbindService(mInternalConnection); |
| 701 | } |
| 702 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 703 | |
| 704 | public void removeAll() { |
| 705 | Slog.e(TAG, "Remove the spell checker bind unexpectedly."); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 706 | synchronized(mSpellCheckerMap) { |
satok | 2520ed8 | 2011-10-31 19:38:05 +0900 | [diff] [blame] | 707 | final int size = mListeners.size(); |
| 708 | for (int i = 0; i < size; ++i) { |
| 709 | final InternalDeathRecipient idr = mListeners.get(i); |
| 710 | idr.mScListener.asBinder().unlinkToDeath(idr, 0); |
| 711 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 712 | mListeners.clear(); |
| 713 | cleanLocked(); |
| 714 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 715 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | private class InternalServiceConnection implements ServiceConnection { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 719 | private final String mSciId; |
| 720 | private final String mLocale; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 721 | private final Bundle mBundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 722 | public InternalServiceConnection( |
satok | 060677f | 2011-11-17 09:40:56 +0900 | [diff] [blame] | 723 | String id, String locale, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 724 | mSciId = id; |
| 725 | mLocale = locale; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 726 | mBundle = bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | @Override |
| 730 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 731 | synchronized(mSpellCheckerMap) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 732 | if (DBG) { |
| 733 | Slog.w(TAG, "onServiceConnected: " + name); |
| 734 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 735 | ISpellCheckerService spellChecker = ISpellCheckerService.Stub.asInterface(service); |
| 736 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
Dianne Hackborn | c7d233d | 2011-10-19 16:55:27 -0700 | [diff] [blame] | 737 | if (group != null && this == group.mInternalConnection) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 738 | group.onServiceConnected(spellChecker); |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | @Override |
| 744 | public void onServiceDisconnected(ComponentName name) { |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 745 | synchronized(mSpellCheckerMap) { |
| 746 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
satok | 2cf1cf0 | 2011-10-21 15:25:23 +0900 | [diff] [blame] | 747 | if (group != null && this == group.mInternalConnection) { |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 748 | mSpellCheckerBindGroups.remove(mSciId); |
| 749 | } |
| 750 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
| 754 | private class InternalDeathRecipient implements IBinder.DeathRecipient { |
| 755 | public final ITextServicesSessionListener mTsListener; |
| 756 | public final ISpellCheckerSessionListener mScListener; |
| 757 | public final String mScLocale; |
| 758 | private final SpellCheckerBindGroup mGroup; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 759 | public final int mUid; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 760 | public final Bundle mBundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 761 | public InternalDeathRecipient(SpellCheckerBindGroup group, |
| 762 | ITextServicesSessionListener tsListener, String scLocale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 763 | ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 764 | mTsListener = tsListener; |
| 765 | mScListener = scListener; |
| 766 | mScLocale = scLocale; |
| 767 | mGroup = group; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 768 | mUid = uid; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 769 | mBundle = bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 773 | return listener.asBinder().equals(mScListener.asBinder()); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | @Override |
| 777 | public void binderDied() { |
| 778 | mGroup.removeListener(mScListener); |
| 779 | } |
| 780 | } |
| 781 | } |