| 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; |
| 33 | import android.os.IBinder; |
| 34 | import android.os.RemoteException; |
| 35 | import android.os.SystemClock; |
| 36 | import android.provider.Settings; |
| 37 | import android.text.TextUtils; |
| 38 | import android.service.textservice.SpellCheckerService; |
| 39 | import android.util.Log; |
| 40 | import android.util.Slog; |
| 41 | import android.view.textservice.SpellCheckerInfo; |
| 42 | |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.HashMap; |
| 45 | import java.util.HashSet; |
| 46 | import java.util.List; |
| 47 | |
| 48 | public class TextServicesManagerService extends ITextServicesManager.Stub { |
| 49 | private static final String TAG = TextServicesManagerService.class.getSimpleName(); |
| 50 | private static final boolean DBG = false; |
| 51 | |
| 52 | private final Context mContext; |
| 53 | private boolean mSystemReady; |
| 54 | private final TextServicesMonitor mMonitor; |
| 55 | private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap = |
| 56 | new HashMap<String, SpellCheckerInfo>(); |
| 57 | private final ArrayList<SpellCheckerInfo> mSpellCheckerList = new ArrayList<SpellCheckerInfo>(); |
| 58 | private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups = |
| 59 | new HashMap<String, SpellCheckerBindGroup>(); |
| 60 | |
| 61 | public void systemReady() { |
| 62 | if (!mSystemReady) { |
| 63 | mSystemReady = true; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public TextServicesManagerService(Context context) { |
| 68 | mSystemReady = false; |
| 69 | mContext = context; |
| 70 | mMonitor = new TextServicesMonitor(); |
| 71 | mMonitor.register(context, true); |
| 72 | synchronized (mSpellCheckerMap) { |
| 73 | buildSpellCheckerMapLocked(context, mSpellCheckerList, mSpellCheckerMap); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private class TextServicesMonitor extends PackageMonitor { |
| 78 | @Override |
| 79 | public void onSomePackagesChanged() { |
| 80 | synchronized (mSpellCheckerMap) { |
| 81 | buildSpellCheckerMapLocked(mContext, mSpellCheckerList, mSpellCheckerMap); |
| 82 | // TODO: Update for each locale |
| 83 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 84 | if (sci == null) return; |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 85 | final String packageName = sci.getPackageName(); |
| 86 | final int change = isPackageDisappearing(packageName); |
| 87 | if (change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE) { |
| 88 | // Package disappearing |
| 89 | setCurrentSpellChecker(findAvailSpellCheckerLocked(null, packageName)); |
| 90 | } else if (isPackageModified(packageName)) { |
| 91 | // Package modified |
| 92 | setCurrentSpellChecker(findAvailSpellCheckerLocked(null, packageName)); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private static void buildSpellCheckerMapLocked(Context context, |
| 99 | ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map) { |
| 100 | list.clear(); |
| 101 | map.clear(); |
| 102 | final PackageManager pm = context.getPackageManager(); |
| 103 | List<ResolveInfo> services = pm.queryIntentServices( |
| 104 | new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA); |
| 105 | final int N = services.size(); |
| 106 | for (int i = 0; i < N; ++i) { |
| 107 | final ResolveInfo ri = services.get(i); |
| 108 | final ServiceInfo si = ri.serviceInfo; |
| 109 | final ComponentName compName = new ComponentName(si.packageName, si.name); |
| 110 | if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) { |
| 111 | Slog.w(TAG, "Skipping text service " + compName |
| 112 | + ": it does not require the permission " |
| 113 | + android.Manifest.permission.BIND_TEXT_SERVICE); |
| 114 | continue; |
| 115 | } |
| 116 | if (DBG) Slog.d(TAG, "Add: " + compName); |
| 117 | final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri); |
| 118 | list.add(sci); |
| 119 | map.put(sci.getId(), sci); |
| 120 | } |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 121 | if (DBG) { |
| 122 | Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size()); |
| 123 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // TODO: find an appropriate spell checker for specified locale |
| 127 | private SpellCheckerInfo findAvailSpellCheckerLocked(String locale, String prefPackage) { |
| 128 | final int spellCheckersCount = mSpellCheckerList.size(); |
| 129 | if (spellCheckersCount == 0) { |
| 130 | Slog.w(TAG, "no available spell checker services found"); |
| 131 | return null; |
| 132 | } |
| 133 | if (prefPackage != null) { |
| 134 | for (int i = 0; i < spellCheckersCount; ++i) { |
| 135 | final SpellCheckerInfo sci = mSpellCheckerList.get(i); |
| 136 | if (prefPackage.equals(sci.getPackageName())) { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 137 | if (DBG) { |
| 138 | Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName()); |
| 139 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 140 | return sci; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | if (spellCheckersCount > 1) { |
| 145 | Slog.w(TAG, "more than one spell checker service found, picking first"); |
| 146 | } |
| 147 | return mSpellCheckerList.get(0); |
| 148 | } |
| 149 | |
| 150 | // TODO: Save SpellCheckerService by supported languages. Currently only one spell |
| 151 | // checker is saved. |
| 152 | @Override |
| 153 | public SpellCheckerInfo getCurrentSpellChecker(String locale) { |
| 154 | synchronized (mSpellCheckerMap) { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 155 | String curSpellCheckerId = |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 156 | Settings.Secure.getString(mContext.getContentResolver(), |
| 157 | Settings.Secure.SPELL_CHECKER_SERVICE); |
| satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 158 | if (DBG) { |
| 159 | Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId); |
| 160 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 161 | if (TextUtils.isEmpty(curSpellCheckerId)) { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 162 | final SpellCheckerInfo sci = findAvailSpellCheckerLocked(null, null); |
| 163 | if (sci == null) return null; |
| 164 | // Set the current spell checker if there is one or more spell checkers |
| 165 | // available. In this case, "sci" is the first one in the available spell |
| 166 | // checkers. |
| 167 | setCurrentSpellChecker(sci); |
| 168 | return sci; |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 169 | } |
| 170 | return mSpellCheckerMap.get(curSpellCheckerId); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void getSpellCheckerService(SpellCheckerInfo info, String locale, |
| 176 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener) { |
| 177 | if (!mSystemReady) { |
| 178 | return; |
| 179 | } |
| 180 | if (info == null || tsListener == null) { |
| 181 | Slog.e(TAG, "getSpellCheckerService: Invalid input."); |
| 182 | return; |
| 183 | } |
| 184 | final String sciId = info.getId(); |
| 185 | synchronized(mSpellCheckerMap) { |
| 186 | if (!mSpellCheckerMap.containsKey(sciId)) { |
| 187 | return; |
| 188 | } |
| 189 | if (mSpellCheckerBindGroups.containsKey(sciId)) { |
| 190 | mSpellCheckerBindGroups.get(sciId).addListener(tsListener, locale, scListener); |
| 191 | return; |
| 192 | } |
| 193 | final InternalServiceConnection connection = new InternalServiceConnection( |
| 194 | sciId, locale, scListener); |
| 195 | final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE); |
| 196 | serviceIntent.setComponent(info.getComponent()); |
| 197 | if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 198 | Slog.e(TAG, "Failed to get a spell checker service."); |
| 199 | return; |
| 200 | } |
| 201 | final SpellCheckerBindGroup group = new SpellCheckerBindGroup( |
| 202 | connection, tsListener, locale, scListener); |
| 203 | mSpellCheckerBindGroups.put(sciId, group); |
| 204 | } |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | @Override |
| satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 209 | public SpellCheckerInfo[] getEnabledSpellCheckers() { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 210 | if (DBG) { |
| 211 | Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size()); |
| 212 | for (int i = 0; i < mSpellCheckerList.size(); ++i) { |
| 213 | Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName()); |
| 214 | } |
| 215 | } |
| satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 216 | return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]); |
| 217 | } |
| 218 | |
| 219 | @Override |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 220 | public void finishSpellCheckerService(ISpellCheckerSessionListener listener) { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 221 | if (DBG) { |
| 222 | Slog.d(TAG, "FinishSpellCheckerService"); |
| 223 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 224 | synchronized(mSpellCheckerMap) { |
| 225 | for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) { |
| 226 | if (group == null) continue; |
| 227 | group.removeListener(listener); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | private void setCurrentSpellChecker(SpellCheckerInfo sci) { |
| satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 233 | if (DBG) { |
| 234 | Slog.w(TAG, "setCurrentSpellChecker: " + sci.getId()); |
| 235 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 236 | if (sci == null || mSpellCheckerMap.containsKey(sci.getId())) return; |
| 237 | Settings.Secure.putString(mContext.getContentResolver(), |
| 238 | Settings.Secure.SPELL_CHECKER_SERVICE, sci == null ? "" : sci.getId()); |
| 239 | } |
| 240 | |
| 241 | // SpellCheckerBindGroup contains active text service session listeners. |
| 242 | // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from |
| 243 | // mSpellCheckerBindGroups |
| 244 | private class SpellCheckerBindGroup { |
| 245 | final InternalServiceConnection mInternalConnection; |
| 246 | final ArrayList<InternalDeathRecipient> mListeners = |
| 247 | new ArrayList<InternalDeathRecipient>(); |
| 248 | |
| 249 | public SpellCheckerBindGroup(InternalServiceConnection connection, |
| 250 | ITextServicesSessionListener listener, String locale, |
| 251 | ISpellCheckerSessionListener scListener) { |
| 252 | mInternalConnection = connection; |
| 253 | addListener(listener, locale, scListener); |
| 254 | } |
| 255 | |
| 256 | public void onServiceConnected(ISpellCheckerService spellChecker) { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 257 | if (DBG) { |
| 258 | Slog.d(TAG, "onServiceConnected"); |
| 259 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 260 | synchronized(mSpellCheckerMap) { |
| 261 | for (InternalDeathRecipient listener : mListeners) { |
| 262 | try { |
| 263 | final ISpellCheckerSession session = spellChecker.getISpellCheckerSession( |
| 264 | listener.mScLocale, listener.mScListener); |
| 265 | listener.mTsListener.onServiceConnected(session); |
| 266 | } catch (RemoteException e) { |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | public void addListener(ITextServicesSessionListener tsListener, String locale, |
| 273 | ISpellCheckerSessionListener scListener) { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 274 | if (DBG) { |
| 275 | Slog.d(TAG, "addListener: " + locale); |
| 276 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 277 | synchronized(mSpellCheckerMap) { |
| 278 | try { |
| 279 | final int size = mListeners.size(); |
| 280 | for (int i = 0; i < size; ++i) { |
| 281 | if (mListeners.get(i).hasSpellCheckerListener(scListener)) { |
| 282 | // do not add the lister if the group already contains this. |
| 283 | return; |
| 284 | } |
| 285 | } |
| 286 | final InternalDeathRecipient recipient = new InternalDeathRecipient( |
| 287 | this, tsListener, locale, scListener); |
| 288 | scListener.asBinder().linkToDeath(recipient, 0); |
| 289 | mListeners.add(new InternalDeathRecipient( |
| 290 | this, tsListener, locale, scListener)); |
| 291 | } catch(RemoteException e) { |
| 292 | // do nothing |
| 293 | } |
| 294 | cleanLocked(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | public void removeListener(ISpellCheckerSessionListener listener) { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 299 | if (DBG) { |
| 300 | Slog.d(TAG, "remove listener"); |
| 301 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 302 | synchronized(mSpellCheckerMap) { |
| 303 | final int size = mListeners.size(); |
| 304 | final ArrayList<InternalDeathRecipient> removeList = |
| 305 | new ArrayList<InternalDeathRecipient>(); |
| 306 | for (int i = 0; i < size; ++i) { |
| 307 | final InternalDeathRecipient tempRecipient = mListeners.get(i); |
| 308 | if(tempRecipient.hasSpellCheckerListener(listener)) { |
| 309 | removeList.add(tempRecipient); |
| 310 | } |
| 311 | } |
| 312 | final int removeSize = removeList.size(); |
| 313 | for (int i = 0; i < removeSize; ++i) { |
| 314 | mListeners.remove(removeList.get(i)); |
| 315 | } |
| 316 | cleanLocked(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private void cleanLocked() { |
| satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 321 | if (DBG) { |
| 322 | Slog.d(TAG, "cleanLocked"); |
| 323 | } |
| satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 324 | if (mListeners.isEmpty()) { |
| 325 | mSpellCheckerBindGroups.remove(this); |
| 326 | // Unbind service when there is no active clients. |
| 327 | mContext.unbindService(mInternalConnection); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | private class InternalServiceConnection implements ServiceConnection { |
| 333 | private final ISpellCheckerSessionListener mListener; |
| 334 | private final String mSciId; |
| 335 | private final String mLocale; |
| 336 | public InternalServiceConnection( |
| 337 | String id, String locale, ISpellCheckerSessionListener listener) { |
| 338 | mSciId = id; |
| 339 | mLocale = locale; |
| 340 | mListener = listener; |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 345 | synchronized(mSpellCheckerMap) { |
| 346 | ISpellCheckerService spellChecker = ISpellCheckerService.Stub.asInterface(service); |
| 347 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
| 348 | if (group != null) { |
| 349 | group.onServiceConnected(spellChecker); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void onServiceDisconnected(ComponentName name) { |
| 356 | mSpellCheckerBindGroups.remove(mSciId); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | private class InternalDeathRecipient implements IBinder.DeathRecipient { |
| 361 | public final ITextServicesSessionListener mTsListener; |
| 362 | public final ISpellCheckerSessionListener mScListener; |
| 363 | public final String mScLocale; |
| 364 | private final SpellCheckerBindGroup mGroup; |
| 365 | public InternalDeathRecipient(SpellCheckerBindGroup group, |
| 366 | ITextServicesSessionListener tsListener, String scLocale, |
| 367 | ISpellCheckerSessionListener scListener) { |
| 368 | mTsListener = tsListener; |
| 369 | mScListener = scListener; |
| 370 | mScLocale = scLocale; |
| 371 | mGroup = group; |
| 372 | } |
| 373 | |
| 374 | public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) { |
| 375 | return mScListener.equals(listener); |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public void binderDied() { |
| 380 | mGroup.removeListener(mScListener); |
| 381 | } |
| 382 | } |
| 383 | } |