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; |
| 43 | import android.view.textservice.SpellCheckerInfo; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 44 | import android.view.textservice.SpellCheckerSubtype; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 45 | |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 46 | import java.io.IOException; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.HashMap; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 49 | import java.util.List; |
| 50 | |
| 51 | public class TextServicesManagerService extends ITextServicesManager.Stub { |
| 52 | private static final String TAG = TextServicesManagerService.class.getSimpleName(); |
| 53 | private static final boolean DBG = false; |
| 54 | |
| 55 | private final Context mContext; |
| 56 | private boolean mSystemReady; |
| 57 | private final TextServicesMonitor mMonitor; |
| 58 | private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap = |
| 59 | new HashMap<String, SpellCheckerInfo>(); |
| 60 | private final ArrayList<SpellCheckerInfo> mSpellCheckerList = new ArrayList<SpellCheckerInfo>(); |
| 61 | private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups = |
| 62 | new HashMap<String, SpellCheckerBindGroup>(); |
| 63 | |
| 64 | public void systemReady() { |
| 65 | if (!mSystemReady) { |
| 66 | mSystemReady = true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public TextServicesManagerService(Context context) { |
| 71 | mSystemReady = false; |
| 72 | mContext = context; |
| 73 | mMonitor = new TextServicesMonitor(); |
| 74 | mMonitor.register(context, true); |
| 75 | synchronized (mSpellCheckerMap) { |
| 76 | buildSpellCheckerMapLocked(context, mSpellCheckerList, mSpellCheckerMap); |
| 77 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 78 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
| 79 | if (sci == null) { |
| 80 | sci = findAvailSpellCheckerLocked(null, null); |
| 81 | if (sci != null) { |
| 82 | // Set the current spell checker if there is one or more spell checkers |
| 83 | // available. In this case, "sci" is the first one in the available spell |
| 84 | // checkers. |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 85 | setCurrentSpellCheckerLocked(sci.getId()); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 86 | } |
| 87 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private class TextServicesMonitor extends PackageMonitor { |
| 91 | @Override |
| 92 | public void onSomePackagesChanged() { |
| 93 | synchronized (mSpellCheckerMap) { |
| 94 | buildSpellCheckerMapLocked(mContext, mSpellCheckerList, mSpellCheckerMap); |
| 95 | // TODO: Update for each locale |
| 96 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 97 | if (sci == null) return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 98 | final String packageName = sci.getPackageName(); |
| 99 | final int change = isPackageDisappearing(packageName); |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 100 | if (// Package disappearing |
| 101 | change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE |
| 102 | // Package modified |
| 103 | || isPackageModified(packageName)) { |
| 104 | sci = findAvailSpellCheckerLocked(null, packageName); |
| 105 | if (sci != null) { |
| 106 | setCurrentSpellCheckerLocked(sci.getId()); |
| 107 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private static void buildSpellCheckerMapLocked(Context context, |
| 114 | ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map) { |
| 115 | list.clear(); |
| 116 | map.clear(); |
| 117 | final PackageManager pm = context.getPackageManager(); |
| 118 | List<ResolveInfo> services = pm.queryIntentServices( |
| 119 | new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA); |
| 120 | final int N = services.size(); |
| 121 | for (int i = 0; i < N; ++i) { |
| 122 | final ResolveInfo ri = services.get(i); |
| 123 | final ServiceInfo si = ri.serviceInfo; |
| 124 | final ComponentName compName = new ComponentName(si.packageName, si.name); |
| 125 | if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) { |
| 126 | Slog.w(TAG, "Skipping text service " + compName |
| 127 | + ": it does not require the permission " |
| 128 | + android.Manifest.permission.BIND_TEXT_SERVICE); |
| 129 | continue; |
| 130 | } |
| 131 | if (DBG) Slog.d(TAG, "Add: " + compName); |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 132 | try { |
| 133 | final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri); |
| 134 | list.add(sci); |
| 135 | map.put(sci.getId(), sci); |
| 136 | } catch (XmlPullParserException e) { |
| 137 | Slog.w(TAG, "Unable to load the spell checker " + compName, e); |
| 138 | } catch (IOException e) { |
| 139 | Slog.w(TAG, "Unable to load the spell checker " + compName, e); |
| 140 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 141 | } |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 142 | if (DBG) { |
| 143 | Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size()); |
| 144 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // TODO: find an appropriate spell checker for specified locale |
| 148 | private SpellCheckerInfo findAvailSpellCheckerLocked(String locale, String prefPackage) { |
| 149 | final int spellCheckersCount = mSpellCheckerList.size(); |
| 150 | if (spellCheckersCount == 0) { |
| 151 | Slog.w(TAG, "no available spell checker services found"); |
| 152 | return null; |
| 153 | } |
| 154 | if (prefPackage != null) { |
| 155 | for (int i = 0; i < spellCheckersCount; ++i) { |
| 156 | final SpellCheckerInfo sci = mSpellCheckerList.get(i); |
| 157 | if (prefPackage.equals(sci.getPackageName())) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 158 | if (DBG) { |
| 159 | Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName()); |
| 160 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 161 | return sci; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | if (spellCheckersCount > 1) { |
| 166 | Slog.w(TAG, "more than one spell checker service found, picking first"); |
| 167 | } |
| 168 | return mSpellCheckerList.get(0); |
| 169 | } |
| 170 | |
| 171 | // TODO: Save SpellCheckerService by supported languages. Currently only one spell |
| 172 | // checker is saved. |
| 173 | @Override |
| 174 | public SpellCheckerInfo getCurrentSpellChecker(String locale) { |
| 175 | synchronized (mSpellCheckerMap) { |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 176 | final String curSpellCheckerId = |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 177 | Settings.Secure.getString(mContext.getContentResolver(), |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 178 | Settings.Secure.SELECTED_SPELL_CHECKER); |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 179 | if (DBG) { |
| 180 | Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId); |
| 181 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 182 | if (TextUtils.isEmpty(curSpellCheckerId)) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 183 | return null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 184 | } |
| 185 | return mSpellCheckerMap.get(curSpellCheckerId); |
| 186 | } |
| 187 | } |
| 188 | |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 189 | // TODO: Save SpellCheckerSubtype by supported languages. |
| 190 | @Override |
| 191 | public SpellCheckerSubtype getCurrentSpellCheckerSubtype(String locale) { |
| 192 | synchronized (mSpellCheckerMap) { |
| 193 | final String subtypeHashCodeStr = |
| 194 | Settings.Secure.getString(mContext.getContentResolver(), |
| 195 | Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE); |
| 196 | if (DBG) { |
| 197 | Slog.w(TAG, "getCurrentSpellChecker: " + subtypeHashCodeStr); |
| 198 | } |
| 199 | final SpellCheckerInfo sci = getCurrentSpellChecker(null); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 200 | if (sci == null || sci.getSubtypeCount() == 0) { |
| 201 | if (DBG) { |
| 202 | Slog.w(TAG, "Subtype not found."); |
| 203 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 204 | return null; |
| 205 | } |
| 206 | if (TextUtils.isEmpty(subtypeHashCodeStr)) { |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 207 | if (DBG) { |
| 208 | Slog.w(TAG, "Return first subtype in " + sci.getId()); |
| 209 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 210 | // Return the first Subtype if there is no settings for the current subtype. |
| 211 | return sci.getSubtypeAt(0); |
| 212 | } |
| 213 | final int hashCode = Integer.valueOf(subtypeHashCodeStr); |
| 214 | for (int i = 0; i < sci.getSubtypeCount(); ++i) { |
| 215 | final SpellCheckerSubtype scs = sci.getSubtypeAt(i); |
| 216 | if (scs.hashCode() == hashCode) { |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 217 | if (DBG) { |
| 218 | Slog.w(TAG, "Return subtype " + scs.hashCode()); |
| 219 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 220 | return scs; |
| 221 | } |
| 222 | } |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 223 | if (DBG) { |
| 224 | Slog.w(TAG, "Return first subtype in " + sci.getId()); |
| 225 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 226 | return sci.getSubtypeAt(0); |
| 227 | } |
| 228 | } |
| 229 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 230 | @Override |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 231 | public void getSpellCheckerService(String sciId, String locale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 232 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener, |
| 233 | Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 234 | if (!mSystemReady) { |
| 235 | return; |
| 236 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 237 | if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 238 | Slog.e(TAG, "getSpellCheckerService: Invalid input."); |
| 239 | return; |
| 240 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 241 | synchronized(mSpellCheckerMap) { |
| 242 | if (!mSpellCheckerMap.containsKey(sciId)) { |
| 243 | return; |
| 244 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 245 | final SpellCheckerInfo sci = mSpellCheckerMap.get(sciId); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 246 | final int uid = Binder.getCallingUid(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 247 | if (mSpellCheckerBindGroups.containsKey(sciId)) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 248 | final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId); |
| 249 | if (bindGroup != null) { |
| 250 | final InternalDeathRecipient recipient = |
| 251 | mSpellCheckerBindGroups.get(sciId).addListener( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 252 | tsListener, locale, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 253 | if (recipient == null) { |
| 254 | if (DBG) { |
| 255 | Slog.w(TAG, "Didn't create a death recipient."); |
| 256 | } |
| 257 | return; |
| 258 | } |
| 259 | if (bindGroup.mSpellChecker == null & bindGroup.mConnected) { |
| 260 | Slog.e(TAG, "The state of the spell checker bind group is illegal."); |
| 261 | bindGroup.removeAll(); |
| 262 | } else if (bindGroup.mSpellChecker != null) { |
| 263 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 264 | Slog.w(TAG, "Existing bind found. Return a spell checker session now. " |
| 265 | + "Listeners count = " + bindGroup.mListeners.size()); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 266 | } |
| 267 | try { |
| 268 | final ISpellCheckerSession session = |
| 269 | bindGroup.mSpellChecker.getISpellCheckerSession( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 270 | recipient.mScLocale, recipient.mScListener, bundle); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 271 | if (session != null) { |
| 272 | tsListener.onServiceConnected(session); |
| 273 | return; |
| 274 | } else { |
| 275 | if (DBG) { |
| 276 | Slog.w(TAG, "Existing bind already expired. "); |
| 277 | } |
| 278 | bindGroup.removeAll(); |
| 279 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 280 | } catch (RemoteException e) { |
| 281 | Slog.e(TAG, "Exception in getting spell checker session: " + e); |
| 282 | bindGroup.removeAll(); |
| 283 | } |
| 284 | } |
| 285 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 286 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 287 | final long ident = Binder.clearCallingIdentity(); |
| 288 | try { |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 289 | startSpellCheckerServiceInnerLocked( |
| 290 | sci, locale, tsListener, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 291 | } finally { |
| 292 | Binder.restoreCallingIdentity(ident); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 293 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 294 | } |
| 295 | return; |
| 296 | } |
| 297 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 298 | @Override |
| 299 | public boolean isSpellCheckerEnabled() { |
| 300 | synchronized(mSpellCheckerMap) { |
| 301 | return isSpellCheckerEnabledLocked(); |
| 302 | } |
| 303 | } |
| 304 | |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 305 | private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale, |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 306 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 307 | int uid, Bundle bundle) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 308 | if (DBG) { |
| 309 | Slog.w(TAG, "Start spell checker session inner locked."); |
| 310 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 311 | final String sciId = info.getId(); |
| 312 | final InternalServiceConnection connection = new InternalServiceConnection( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 313 | sciId, locale, scListener, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 314 | final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE); |
| 315 | serviceIntent.setComponent(info.getComponent()); |
| 316 | if (DBG) { |
| 317 | Slog.w(TAG, "bind service: " + info.getId()); |
| 318 | } |
| 319 | if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 320 | Slog.e(TAG, "Failed to get a spell checker service."); |
| 321 | return; |
| 322 | } |
| 323 | final SpellCheckerBindGroup group = new SpellCheckerBindGroup( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 324 | connection, tsListener, locale, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 325 | mSpellCheckerBindGroups.put(sciId, group); |
| 326 | } |
| 327 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 328 | @Override |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 329 | public SpellCheckerInfo[] getEnabledSpellCheckers() { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 330 | if (DBG) { |
| 331 | Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size()); |
| 332 | for (int i = 0; i < mSpellCheckerList.size(); ++i) { |
| 333 | Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName()); |
| 334 | } |
| 335 | } |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 336 | return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]); |
| 337 | } |
| 338 | |
| 339 | @Override |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 340 | public void finishSpellCheckerService(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 341 | if (DBG) { |
| 342 | Slog.d(TAG, "FinishSpellCheckerService"); |
| 343 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 344 | synchronized(mSpellCheckerMap) { |
| 345 | for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) { |
| 346 | if (group == null) continue; |
| 347 | group.removeListener(listener); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 352 | @Override |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 353 | public void setCurrentSpellChecker(String locale, String sciId) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 354 | synchronized(mSpellCheckerMap) { |
| 355 | if (mContext.checkCallingOrSelfPermission( |
| 356 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 357 | != PackageManager.PERMISSION_GRANTED) { |
| 358 | throw new SecurityException( |
| 359 | "Requires permission " |
| 360 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 361 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 362 | setCurrentSpellCheckerLocked(sciId); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 366 | @Override |
| 367 | public void setCurrentSpellCheckerSubtype(String locale, int hashCode) { |
| 368 | synchronized(mSpellCheckerMap) { |
| 369 | if (mContext.checkCallingOrSelfPermission( |
| 370 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 371 | != PackageManager.PERMISSION_GRANTED) { |
| 372 | throw new SecurityException( |
| 373 | "Requires permission " |
| 374 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 375 | } |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 376 | setCurrentSpellCheckerSubtypeLocked(hashCode); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public void setSpellCheckerEnabled(boolean enabled) { |
| 382 | synchronized(mSpellCheckerMap) { |
| 383 | if (mContext.checkCallingOrSelfPermission( |
| 384 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 385 | != PackageManager.PERMISSION_GRANTED) { |
| 386 | throw new SecurityException( |
| 387 | "Requires permission " |
| 388 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 389 | } |
| 390 | setSpellCheckerEnabledLocked(enabled); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 394 | private void setCurrentSpellCheckerLocked(String sciId) { |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 395 | if (DBG) { |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 396 | Slog.w(TAG, "setCurrentSpellChecker: " + sciId); |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 397 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 398 | if (TextUtils.isEmpty(sciId) || !mSpellCheckerMap.containsKey(sciId)) return; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 399 | final long ident = Binder.clearCallingIdentity(); |
| 400 | try { |
| 401 | Settings.Secure.putString(mContext.getContentResolver(), |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 402 | Settings.Secure.SELECTED_SPELL_CHECKER, sciId); |
| 403 | } finally { |
| 404 | Binder.restoreCallingIdentity(ident); |
| 405 | } |
| 406 | } |
| 407 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 408 | private void setCurrentSpellCheckerSubtypeLocked(int hashCode) { |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 409 | if (DBG) { |
| 410 | Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode); |
| 411 | } |
| 412 | final SpellCheckerInfo sci = getCurrentSpellChecker(null); |
| 413 | if (sci == null) return; |
| 414 | boolean found = false; |
| 415 | for (int i = 0; i < sci.getSubtypeCount(); ++i) { |
| 416 | if(sci.getSubtypeAt(i).hashCode() == hashCode) { |
| 417 | found = true; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | if (!found) { |
| 422 | return; |
| 423 | } |
| 424 | final long ident = Binder.clearCallingIdentity(); |
| 425 | try { |
| 426 | Settings.Secure.putString(mContext.getContentResolver(), |
| 427 | Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(hashCode)); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 428 | } finally { |
| 429 | Binder.restoreCallingIdentity(ident); |
| 430 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 431 | } |
| 432 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame^] | 433 | private void setSpellCheckerEnabledLocked(boolean enabled) { |
| 434 | if (DBG) { |
| 435 | Slog.w(TAG, "setSpellCheckerEnabled: " + enabled); |
| 436 | } |
| 437 | final long ident = Binder.clearCallingIdentity(); |
| 438 | try { |
| 439 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 440 | Settings.Secure.SPELL_CHECKER_ENABLED, enabled ? 1 : 0); |
| 441 | } finally { |
| 442 | Binder.restoreCallingIdentity(ident); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | private boolean isSpellCheckerEnabledLocked() { |
| 447 | final long ident = Binder.clearCallingIdentity(); |
| 448 | try { |
| 449 | final boolean retval = Settings.Secure.getInt(mContext.getContentResolver(), |
| 450 | Settings.Secure.SPELL_CHECKER_ENABLED, 1) == 1; |
| 451 | if (DBG) { |
| 452 | Slog.w(TAG, "getSpellCheckerEnabled: " + retval); |
| 453 | } |
| 454 | return retval; |
| 455 | } finally { |
| 456 | Binder.restoreCallingIdentity(ident); |
| 457 | } |
| 458 | } |
| 459 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 460 | // SpellCheckerBindGroup contains active text service session listeners. |
| 461 | // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from |
| 462 | // mSpellCheckerBindGroups |
| 463 | private class SpellCheckerBindGroup { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 464 | private final String TAG = SpellCheckerBindGroup.class.getSimpleName(); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 465 | private final InternalServiceConnection mInternalConnection; |
| 466 | private final ArrayList<InternalDeathRecipient> mListeners = |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 467 | new ArrayList<InternalDeathRecipient>(); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 468 | public ISpellCheckerService mSpellChecker; |
| 469 | public boolean mConnected; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 470 | |
| 471 | public SpellCheckerBindGroup(InternalServiceConnection connection, |
| 472 | ITextServicesSessionListener listener, String locale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 473 | ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 474 | mInternalConnection = connection; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 475 | mConnected = false; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 476 | addListener(listener, locale, scListener, uid, bundle); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | public void onServiceConnected(ISpellCheckerService spellChecker) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 480 | if (DBG) { |
| 481 | Slog.d(TAG, "onServiceConnected"); |
| 482 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 483 | synchronized(mSpellCheckerMap) { |
| 484 | for (InternalDeathRecipient listener : mListeners) { |
| 485 | try { |
| 486 | final ISpellCheckerSession session = spellChecker.getISpellCheckerSession( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 487 | listener.mScLocale, listener.mScListener, listener.mBundle); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 488 | listener.mTsListener.onServiceConnected(session); |
| 489 | } catch (RemoteException e) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 490 | Slog.e(TAG, "Exception in getting the spell checker session: " + e); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 491 | removeAll(); |
| 492 | return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 493 | } |
| 494 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 495 | mSpellChecker = spellChecker; |
| 496 | mConnected = true; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 500 | public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 501 | String locale, ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 502 | if (DBG) { |
| 503 | Slog.d(TAG, "addListener: " + locale); |
| 504 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 505 | InternalDeathRecipient recipient = null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 506 | synchronized(mSpellCheckerMap) { |
| 507 | try { |
| 508 | final int size = mListeners.size(); |
| 509 | for (int i = 0; i < size; ++i) { |
| 510 | if (mListeners.get(i).hasSpellCheckerListener(scListener)) { |
| 511 | // do not add the lister if the group already contains this. |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 512 | return null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 513 | } |
| 514 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 515 | recipient = new InternalDeathRecipient( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 516 | this, tsListener, locale, scListener, uid, bundle); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 517 | scListener.asBinder().linkToDeath(recipient, 0); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 518 | mListeners.add(recipient); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 519 | } catch(RemoteException e) { |
| 520 | // do nothing |
| 521 | } |
| 522 | cleanLocked(); |
| 523 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 524 | return recipient; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | public void removeListener(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 528 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 529 | Slog.w(TAG, "remove listener: " + listener.hashCode()); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 530 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 531 | synchronized(mSpellCheckerMap) { |
| 532 | final int size = mListeners.size(); |
| 533 | final ArrayList<InternalDeathRecipient> removeList = |
| 534 | new ArrayList<InternalDeathRecipient>(); |
| 535 | for (int i = 0; i < size; ++i) { |
| 536 | final InternalDeathRecipient tempRecipient = mListeners.get(i); |
| 537 | if(tempRecipient.hasSpellCheckerListener(listener)) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 538 | if (DBG) { |
| 539 | Slog.w(TAG, "found existing listener."); |
| 540 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 541 | removeList.add(tempRecipient); |
| 542 | } |
| 543 | } |
| 544 | final int removeSize = removeList.size(); |
| 545 | for (int i = 0; i < removeSize; ++i) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 546 | if (DBG) { |
| 547 | Slog.w(TAG, "Remove " + removeList.get(i)); |
| 548 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 549 | mListeners.remove(removeList.get(i)); |
| 550 | } |
| 551 | cleanLocked(); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | private void cleanLocked() { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 556 | if (DBG) { |
| 557 | Slog.d(TAG, "cleanLocked"); |
| 558 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 559 | if (mListeners.isEmpty()) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 560 | if (mSpellCheckerBindGroups.containsKey(this)) { |
| 561 | mSpellCheckerBindGroups.remove(this); |
| 562 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 563 | // Unbind service when there is no active clients. |
| 564 | mContext.unbindService(mInternalConnection); |
| 565 | } |
| 566 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 567 | |
| 568 | public void removeAll() { |
| 569 | Slog.e(TAG, "Remove the spell checker bind unexpectedly."); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 570 | synchronized(mSpellCheckerMap) { |
| 571 | mListeners.clear(); |
| 572 | cleanLocked(); |
| 573 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 574 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | private class InternalServiceConnection implements ServiceConnection { |
| 578 | private final ISpellCheckerSessionListener mListener; |
| 579 | private final String mSciId; |
| 580 | private final String mLocale; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 581 | private final Bundle mBundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 582 | public InternalServiceConnection( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 583 | String id, String locale, ISpellCheckerSessionListener listener, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 584 | mSciId = id; |
| 585 | mLocale = locale; |
| 586 | mListener = listener; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 587 | mBundle = bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 592 | synchronized(mSpellCheckerMap) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 593 | if (DBG) { |
| 594 | Slog.w(TAG, "onServiceConnected: " + name); |
| 595 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 596 | ISpellCheckerService spellChecker = ISpellCheckerService.Stub.asInterface(service); |
| 597 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
| 598 | if (group != null) { |
| 599 | group.onServiceConnected(spellChecker); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void onServiceDisconnected(ComponentName name) { |
| 606 | mSpellCheckerBindGroups.remove(mSciId); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | private class InternalDeathRecipient implements IBinder.DeathRecipient { |
| 611 | public final ITextServicesSessionListener mTsListener; |
| 612 | public final ISpellCheckerSessionListener mScListener; |
| 613 | public final String mScLocale; |
| 614 | private final SpellCheckerBindGroup mGroup; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 615 | public final int mUid; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 616 | public final Bundle mBundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 617 | public InternalDeathRecipient(SpellCheckerBindGroup group, |
| 618 | ITextServicesSessionListener tsListener, String scLocale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 619 | ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 620 | mTsListener = tsListener; |
| 621 | mScListener = scListener; |
| 622 | mScLocale = scLocale; |
| 623 | mGroup = group; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 624 | mUid = uid; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 625 | mBundle = bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 629 | return listener.asBinder().equals(mScListener.asBinder()); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | @Override |
| 633 | public void binderDied() { |
| 634 | mGroup.removeListener(mScListener); |
| 635 | } |
| 636 | } |
| 637 | } |