blob: 8384ebc23d7d093058f87a4ce427dfad9e058178 [file] [log] [blame]
satok988323c2011-06-22 16:38:13 +09001/*
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
17package com.android.server;
18
19import com.android.internal.content.PackageMonitor;
20import com.android.internal.textservice.ISpellCheckerService;
21import com.android.internal.textservice.ISpellCheckerSession;
22import com.android.internal.textservice.ISpellCheckerSessionListener;
23import com.android.internal.textservice.ITextServicesManager;
24import com.android.internal.textservice.ITextServicesSessionListener;
25
satok03b2ea12011-08-03 17:36:14 +090026import org.xmlpull.v1.XmlPullParserException;
27
satok988323c2011-06-22 16:38:13 +090028import android.content.ComponentName;
29import android.content.Context;
30import android.content.Intent;
31import android.content.ServiceConnection;
32import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
34import android.content.pm.ServiceInfo;
satok6be6d752011-07-28 20:40:38 +090035import android.os.Binder;
satok53578062011-08-03 16:08:59 +090036import android.os.Bundle;
satok988323c2011-06-22 16:38:13 +090037import android.os.IBinder;
38import android.os.RemoteException;
satok988323c2011-06-22 16:38:13 +090039import android.provider.Settings;
satok988323c2011-06-22 16:38:13 +090040import android.service.textservice.SpellCheckerService;
satok53578062011-08-03 16:08:59 +090041import android.text.TextUtils;
satok988323c2011-06-22 16:38:13 +090042import android.util.Slog;
satok05f24702011-11-02 19:29:35 +090043import android.view.inputmethod.InputMethodManager;
44import android.view.inputmethod.InputMethodSubtype;
satok988323c2011-06-22 16:38:13 +090045import android.view.textservice.SpellCheckerInfo;
satokada8c4e2011-08-23 14:56:56 +090046import android.view.textservice.SpellCheckerSubtype;
satok988323c2011-06-22 16:38:13 +090047
Dianne Hackborn71e14da2011-10-16 16:28:10 -070048import java.io.FileDescriptor;
satok03b2ea12011-08-03 17:36:14 +090049import java.io.IOException;
Dianne Hackborn71e14da2011-10-16 16:28:10 -070050import java.io.PrintWriter;
satok988323c2011-06-22 16:38:13 +090051import java.util.ArrayList;
52import java.util.HashMap;
satok988323c2011-06-22 16:38:13 +090053import java.util.List;
Dianne Hackborn71e14da2011-10-16 16:28:10 -070054import java.util.Map;
satok988323c2011-06-22 16:38:13 +090055
56public class TextServicesManagerService extends ITextServicesManager.Stub {
57 private static final String TAG = TextServicesManagerService.class.getSimpleName();
58 private static final boolean DBG = false;
59
60 private final Context mContext;
61 private boolean mSystemReady;
62 private final TextServicesMonitor mMonitor;
63 private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap =
64 new HashMap<String, SpellCheckerInfo>();
65 private final ArrayList<SpellCheckerInfo> mSpellCheckerList = new ArrayList<SpellCheckerInfo>();
66 private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups =
67 new HashMap<String, SpellCheckerBindGroup>();
68
69 public void systemReady() {
70 if (!mSystemReady) {
71 mSystemReady = true;
72 }
73 }
74
75 public TextServicesManagerService(Context context) {
76 mSystemReady = false;
77 mContext = context;
78 mMonitor = new TextServicesMonitor();
79 mMonitor.register(context, true);
80 synchronized (mSpellCheckerMap) {
81 buildSpellCheckerMapLocked(context, mSpellCheckerList, mSpellCheckerMap);
82 }
satokdf5659d2011-07-29 18:38:21 +090083 SpellCheckerInfo sci = getCurrentSpellChecker(null);
84 if (sci == null) {
85 sci = findAvailSpellCheckerLocked(null, null);
86 if (sci != null) {
87 // Set the current spell checker if there is one or more spell checkers
88 // available. In this case, "sci" is the first one in the available spell
89 // checkers.
satok5b9b5a92011-08-02 12:24:44 +090090 setCurrentSpellCheckerLocked(sci.getId());
satokdf5659d2011-07-29 18:38:21 +090091 }
92 }
satok988323c2011-06-22 16:38:13 +090093 }
94
95 private class TextServicesMonitor extends PackageMonitor {
96 @Override
97 public void onSomePackagesChanged() {
98 synchronized (mSpellCheckerMap) {
99 buildSpellCheckerMapLocked(mContext, mSpellCheckerList, mSpellCheckerMap);
100 // TODO: Update for each locale
101 SpellCheckerInfo sci = getCurrentSpellChecker(null);
satokda317ef2011-07-26 08:02:45 +0900102 if (sci == null) return;
satok988323c2011-06-22 16:38:13 +0900103 final String packageName = sci.getPackageName();
104 final int change = isPackageDisappearing(packageName);
satok5b9b5a92011-08-02 12:24:44 +0900105 if (// Package disappearing
106 change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE
107 // Package modified
108 || isPackageModified(packageName)) {
109 sci = findAvailSpellCheckerLocked(null, packageName);
110 if (sci != null) {
111 setCurrentSpellCheckerLocked(sci.getId());
112 }
satok988323c2011-06-22 16:38:13 +0900113 }
114 }
115 }
116 }
117
118 private static void buildSpellCheckerMapLocked(Context context,
119 ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map) {
120 list.clear();
121 map.clear();
122 final PackageManager pm = context.getPackageManager();
123 List<ResolveInfo> services = pm.queryIntentServices(
124 new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA);
125 final int N = services.size();
126 for (int i = 0; i < N; ++i) {
127 final ResolveInfo ri = services.get(i);
128 final ServiceInfo si = ri.serviceInfo;
129 final ComponentName compName = new ComponentName(si.packageName, si.name);
130 if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) {
131 Slog.w(TAG, "Skipping text service " + compName
132 + ": it does not require the permission "
133 + android.Manifest.permission.BIND_TEXT_SERVICE);
134 continue;
135 }
136 if (DBG) Slog.d(TAG, "Add: " + compName);
satok03b2ea12011-08-03 17:36:14 +0900137 try {
138 final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri);
satok3cb5b392011-08-26 11:55:21 +0900139 if (sci.getSubtypeCount() <= 0) {
140 Slog.w(TAG, "Skipping text service " + compName
141 + ": it does not contain subtypes.");
142 continue;
143 }
satok03b2ea12011-08-03 17:36:14 +0900144 list.add(sci);
145 map.put(sci.getId(), sci);
146 } catch (XmlPullParserException e) {
147 Slog.w(TAG, "Unable to load the spell checker " + compName, e);
148 } catch (IOException e) {
149 Slog.w(TAG, "Unable to load the spell checker " + compName, e);
150 }
satok988323c2011-06-22 16:38:13 +0900151 }
satokda317ef2011-07-26 08:02:45 +0900152 if (DBG) {
153 Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size());
154 }
satok988323c2011-06-22 16:38:13 +0900155 }
156
157 // TODO: find an appropriate spell checker for specified locale
158 private SpellCheckerInfo findAvailSpellCheckerLocked(String locale, String prefPackage) {
159 final int spellCheckersCount = mSpellCheckerList.size();
160 if (spellCheckersCount == 0) {
161 Slog.w(TAG, "no available spell checker services found");
162 return null;
163 }
164 if (prefPackage != null) {
165 for (int i = 0; i < spellCheckersCount; ++i) {
166 final SpellCheckerInfo sci = mSpellCheckerList.get(i);
167 if (prefPackage.equals(sci.getPackageName())) {
satokda317ef2011-07-26 08:02:45 +0900168 if (DBG) {
169 Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName());
170 }
satok988323c2011-06-22 16:38:13 +0900171 return sci;
172 }
173 }
174 }
175 if (spellCheckersCount > 1) {
176 Slog.w(TAG, "more than one spell checker service found, picking first");
177 }
178 return mSpellCheckerList.get(0);
179 }
180
181 // TODO: Save SpellCheckerService by supported languages. Currently only one spell
182 // checker is saved.
183 @Override
184 public SpellCheckerInfo getCurrentSpellChecker(String locale) {
185 synchronized (mSpellCheckerMap) {
satoka33c4fc2011-08-25 16:50:11 +0900186 final String curSpellCheckerId =
satok988323c2011-06-22 16:38:13 +0900187 Settings.Secure.getString(mContext.getContentResolver(),
satokada8c4e2011-08-23 14:56:56 +0900188 Settings.Secure.SELECTED_SPELL_CHECKER);
satok562ab582011-07-25 10:12:21 +0900189 if (DBG) {
190 Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId);
191 }
satok988323c2011-06-22 16:38:13 +0900192 if (TextUtils.isEmpty(curSpellCheckerId)) {
satokdf5659d2011-07-29 18:38:21 +0900193 return null;
satok988323c2011-06-22 16:38:13 +0900194 }
195 return mSpellCheckerMap.get(curSpellCheckerId);
196 }
197 }
198
satok3cb5b392011-08-26 11:55:21 +0900199 // TODO: Respect allowImplicitlySelectedSubtype
satokada8c4e2011-08-23 14:56:56 +0900200 // TODO: Save SpellCheckerSubtype by supported languages.
201 @Override
satok3cb5b392011-08-26 11:55:21 +0900202 public SpellCheckerSubtype getCurrentSpellCheckerSubtype(
203 String locale, boolean allowImplicitlySelectedSubtype) {
satokada8c4e2011-08-23 14:56:56 +0900204 synchronized (mSpellCheckerMap) {
205 final String subtypeHashCodeStr =
206 Settings.Secure.getString(mContext.getContentResolver(),
207 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE);
208 if (DBG) {
satokc7b60f722011-08-31 16:30:27 +0900209 Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCodeStr);
satokada8c4e2011-08-23 14:56:56 +0900210 }
211 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satoka33c4fc2011-08-25 16:50:11 +0900212 if (sci == null || sci.getSubtypeCount() == 0) {
213 if (DBG) {
214 Slog.w(TAG, "Subtype not found.");
215 }
satokada8c4e2011-08-23 14:56:56 +0900216 return null;
217 }
satokb3879542011-08-26 17:35:27 +0900218 final int hashCode;
219 if (!TextUtils.isEmpty(subtypeHashCodeStr)) {
220 hashCode = Integer.valueOf(subtypeHashCodeStr);
221 } else {
222 hashCode = 0;
223 }
224 if (hashCode == 0 && !allowImplicitlySelectedSubtype) {
satok3cb5b392011-08-26 11:55:21 +0900225 return null;
satokada8c4e2011-08-23 14:56:56 +0900226 }
satok05f24702011-11-02 19:29:35 +0900227 String candidateLocale = null;
228 if (hashCode == 0) {
229 // Spell checker language settings == "auto"
230 final InputMethodManager imm =
231 (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
232 if (imm != null) {
233 final InputMethodSubtype currentInputMethodSubtype =
234 imm.getCurrentInputMethodSubtype();
235 if (currentInputMethodSubtype != null) {
236 final String localeString = currentInputMethodSubtype.getLocale();
237 if (!TextUtils.isEmpty(localeString)) {
238 // 1. Use keyboard locale if available in the spell checker
239 candidateLocale = localeString;
240 }
241 }
242 }
243 if (candidateLocale == null) {
244 // 2. Use System locale if available in the spell checker
245 candidateLocale = mContext.getResources().getConfiguration().locale.toString();
246 }
247 }
satokb3879542011-08-26 17:35:27 +0900248 SpellCheckerSubtype candidate = null;
satokada8c4e2011-08-23 14:56:56 +0900249 for (int i = 0; i < sci.getSubtypeCount(); ++i) {
250 final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
satokb3879542011-08-26 17:35:27 +0900251 if (hashCode == 0) {
satok05f24702011-11-02 19:29:35 +0900252 if (candidateLocale.equals(locale)) {
satokb3879542011-08-26 17:35:27 +0900253 return scs;
254 } else if (candidate == null) {
255 final String scsLocale = scs.getLocale();
satok05f24702011-11-02 19:29:35 +0900256 if (candidateLocale.length() >= 2
satokb3879542011-08-26 17:35:27 +0900257 && scsLocale.length() >= 2
satok05f24702011-11-02 19:29:35 +0900258 && candidateLocale.substring(0, 2).equals(
satokb3879542011-08-26 17:35:27 +0900259 scsLocale.substring(0, 2))) {
satok05f24702011-11-02 19:29:35 +0900260 // Fall back to the applicable language
satokb3879542011-08-26 17:35:27 +0900261 candidate = scs;
262 }
263 }
264 } else if (scs.hashCode() == hashCode) {
satoka33c4fc2011-08-25 16:50:11 +0900265 if (DBG) {
satok70deff42011-09-30 15:14:21 +0900266 Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
267 + ", " + scs.getLocale());
satoka33c4fc2011-08-25 16:50:11 +0900268 }
satok05f24702011-11-02 19:29:35 +0900269 // 3. Use the user specified spell check language
satokada8c4e2011-08-23 14:56:56 +0900270 return scs;
271 }
272 }
satok05f24702011-11-02 19:29:35 +0900273 // 4. Fall back to the applicable language and return it if not null
274 // 5. Simply just return it even if it's null which means we could find no suitable
275 // spell check languages
satokb3879542011-08-26 17:35:27 +0900276 return candidate;
satokada8c4e2011-08-23 14:56:56 +0900277 }
278 }
279
satok988323c2011-06-22 16:38:13 +0900280 @Override
satok5b9b5a92011-08-02 12:24:44 +0900281 public void getSpellCheckerService(String sciId, String locale,
satok53578062011-08-03 16:08:59 +0900282 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
283 Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900284 if (!mSystemReady) {
285 return;
286 }
satok5b9b5a92011-08-02 12:24:44 +0900287 if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) {
satok988323c2011-06-22 16:38:13 +0900288 Slog.e(TAG, "getSpellCheckerService: Invalid input.");
289 return;
290 }
satok988323c2011-06-22 16:38:13 +0900291 synchronized(mSpellCheckerMap) {
292 if (!mSpellCheckerMap.containsKey(sciId)) {
293 return;
294 }
satok5b9b5a92011-08-02 12:24:44 +0900295 final SpellCheckerInfo sci = mSpellCheckerMap.get(sciId);
satokdf5659d2011-07-29 18:38:21 +0900296 final int uid = Binder.getCallingUid();
satok988323c2011-06-22 16:38:13 +0900297 if (mSpellCheckerBindGroups.containsKey(sciId)) {
satok6be6d752011-07-28 20:40:38 +0900298 final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId);
299 if (bindGroup != null) {
300 final InternalDeathRecipient recipient =
301 mSpellCheckerBindGroups.get(sciId).addListener(
satok53578062011-08-03 16:08:59 +0900302 tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900303 if (recipient == null) {
304 if (DBG) {
305 Slog.w(TAG, "Didn't create a death recipient.");
306 }
307 return;
308 }
309 if (bindGroup.mSpellChecker == null & bindGroup.mConnected) {
310 Slog.e(TAG, "The state of the spell checker bind group is illegal.");
311 bindGroup.removeAll();
312 } else if (bindGroup.mSpellChecker != null) {
313 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900314 Slog.w(TAG, "Existing bind found. Return a spell checker session now. "
315 + "Listeners count = " + bindGroup.mListeners.size());
satok6be6d752011-07-28 20:40:38 +0900316 }
317 try {
318 final ISpellCheckerSession session =
319 bindGroup.mSpellChecker.getISpellCheckerSession(
satok53578062011-08-03 16:08:59 +0900320 recipient.mScLocale, recipient.mScListener, bundle);
satokdf5659d2011-07-29 18:38:21 +0900321 if (session != null) {
322 tsListener.onServiceConnected(session);
323 return;
324 } else {
325 if (DBG) {
326 Slog.w(TAG, "Existing bind already expired. ");
327 }
328 bindGroup.removeAll();
329 }
satok6be6d752011-07-28 20:40:38 +0900330 } catch (RemoteException e) {
331 Slog.e(TAG, "Exception in getting spell checker session: " + e);
332 bindGroup.removeAll();
333 }
334 }
335 }
satok988323c2011-06-22 16:38:13 +0900336 }
satok6be6d752011-07-28 20:40:38 +0900337 final long ident = Binder.clearCallingIdentity();
338 try {
satok53578062011-08-03 16:08:59 +0900339 startSpellCheckerServiceInnerLocked(
340 sci, locale, tsListener, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900341 } finally {
342 Binder.restoreCallingIdentity(ident);
satok988323c2011-06-22 16:38:13 +0900343 }
satok988323c2011-06-22 16:38:13 +0900344 }
345 return;
346 }
347
satoka33c4fc2011-08-25 16:50:11 +0900348 @Override
349 public boolean isSpellCheckerEnabled() {
350 synchronized(mSpellCheckerMap) {
351 return isSpellCheckerEnabledLocked();
352 }
353 }
354
satok6be6d752011-07-28 20:40:38 +0900355 private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale,
satokdf5659d2011-07-29 18:38:21 +0900356 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
satok53578062011-08-03 16:08:59 +0900357 int uid, Bundle bundle) {
satokdf5659d2011-07-29 18:38:21 +0900358 if (DBG) {
359 Slog.w(TAG, "Start spell checker session inner locked.");
360 }
satok6be6d752011-07-28 20:40:38 +0900361 final String sciId = info.getId();
362 final InternalServiceConnection connection = new InternalServiceConnection(
satok060677f2011-11-17 09:40:56 +0900363 sciId, locale, bundle);
satok6be6d752011-07-28 20:40:38 +0900364 final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE);
365 serviceIntent.setComponent(info.getComponent());
366 if (DBG) {
367 Slog.w(TAG, "bind service: " + info.getId());
368 }
369 if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) {
370 Slog.e(TAG, "Failed to get a spell checker service.");
371 return;
372 }
373 final SpellCheckerBindGroup group = new SpellCheckerBindGroup(
satok53578062011-08-03 16:08:59 +0900374 connection, tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900375 mSpellCheckerBindGroups.put(sciId, group);
376 }
377
satok988323c2011-06-22 16:38:13 +0900378 @Override
satok562ab582011-07-25 10:12:21 +0900379 public SpellCheckerInfo[] getEnabledSpellCheckers() {
satokda317ef2011-07-26 08:02:45 +0900380 if (DBG) {
381 Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size());
382 for (int i = 0; i < mSpellCheckerList.size(); ++i) {
383 Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName());
384 }
385 }
satok562ab582011-07-25 10:12:21 +0900386 return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]);
387 }
388
389 @Override
satok988323c2011-06-22 16:38:13 +0900390 public void finishSpellCheckerService(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900391 if (DBG) {
392 Slog.d(TAG, "FinishSpellCheckerService");
393 }
satok988323c2011-06-22 16:38:13 +0900394 synchronized(mSpellCheckerMap) {
satok4c3fa642011-11-30 18:17:59 +0900395 final ArrayList<SpellCheckerBindGroup> removeList =
396 new ArrayList<SpellCheckerBindGroup>();
satok988323c2011-06-22 16:38:13 +0900397 for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) {
398 if (group == null) continue;
satok4c3fa642011-11-30 18:17:59 +0900399 // Use removeList to avoid modifying mSpellCheckerBindGroups in this loop.
400 removeList.add(group);
401 }
402 final int removeSize = removeList.size();
403 for (int i = 0; i < removeSize; ++i) {
404 removeList.get(i).removeListener(listener);
satok988323c2011-06-22 16:38:13 +0900405 }
406 }
407 }
408
satokdf5659d2011-07-29 18:38:21 +0900409 @Override
satokada8c4e2011-08-23 14:56:56 +0900410 public void setCurrentSpellChecker(String locale, String sciId) {
satokdf5659d2011-07-29 18:38:21 +0900411 synchronized(mSpellCheckerMap) {
412 if (mContext.checkCallingOrSelfPermission(
413 android.Manifest.permission.WRITE_SECURE_SETTINGS)
414 != PackageManager.PERMISSION_GRANTED) {
415 throw new SecurityException(
416 "Requires permission "
417 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
418 }
satok5b9b5a92011-08-02 12:24:44 +0900419 setCurrentSpellCheckerLocked(sciId);
satokdf5659d2011-07-29 18:38:21 +0900420 }
421 }
422
satokada8c4e2011-08-23 14:56:56 +0900423 @Override
424 public void setCurrentSpellCheckerSubtype(String locale, int hashCode) {
425 synchronized(mSpellCheckerMap) {
426 if (mContext.checkCallingOrSelfPermission(
427 android.Manifest.permission.WRITE_SECURE_SETTINGS)
428 != PackageManager.PERMISSION_GRANTED) {
429 throw new SecurityException(
430 "Requires permission "
431 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
432 }
satoka33c4fc2011-08-25 16:50:11 +0900433 setCurrentSpellCheckerSubtypeLocked(hashCode);
434 }
435 }
436
437 @Override
438 public void setSpellCheckerEnabled(boolean enabled) {
439 synchronized(mSpellCheckerMap) {
440 if (mContext.checkCallingOrSelfPermission(
441 android.Manifest.permission.WRITE_SECURE_SETTINGS)
442 != PackageManager.PERMISSION_GRANTED) {
443 throw new SecurityException(
444 "Requires permission "
445 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
446 }
447 setSpellCheckerEnabledLocked(enabled);
satokada8c4e2011-08-23 14:56:56 +0900448 }
449 }
450
satok5b9b5a92011-08-02 12:24:44 +0900451 private void setCurrentSpellCheckerLocked(String sciId) {
satok562ab582011-07-25 10:12:21 +0900452 if (DBG) {
satok5b9b5a92011-08-02 12:24:44 +0900453 Slog.w(TAG, "setCurrentSpellChecker: " + sciId);
satok562ab582011-07-25 10:12:21 +0900454 }
satok5b9b5a92011-08-02 12:24:44 +0900455 if (TextUtils.isEmpty(sciId) || !mSpellCheckerMap.containsKey(sciId)) return;
satokf39daef2011-08-26 19:54:27 +0900456 final SpellCheckerInfo currentSci = getCurrentSpellChecker(null);
457 if (currentSci != null && currentSci.getId().equals(sciId)) {
458 // Do nothing if the current spell checker is same as new spell checker.
459 return;
460 }
satokdf5659d2011-07-29 18:38:21 +0900461 final long ident = Binder.clearCallingIdentity();
462 try {
463 Settings.Secure.putString(mContext.getContentResolver(),
satokada8c4e2011-08-23 14:56:56 +0900464 Settings.Secure.SELECTED_SPELL_CHECKER, sciId);
satokf39daef2011-08-26 19:54:27 +0900465 setCurrentSpellCheckerSubtypeLocked(0);
satokada8c4e2011-08-23 14:56:56 +0900466 } finally {
467 Binder.restoreCallingIdentity(ident);
468 }
469 }
470
satoka33c4fc2011-08-25 16:50:11 +0900471 private void setCurrentSpellCheckerSubtypeLocked(int hashCode) {
satokada8c4e2011-08-23 14:56:56 +0900472 if (DBG) {
473 Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode);
474 }
475 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satokfbedf1a2011-08-26 15:48:50 +0900476 int tempHashCode = 0;
477 for (int i = 0; sci != null && i < sci.getSubtypeCount(); ++i) {
satokada8c4e2011-08-23 14:56:56 +0900478 if(sci.getSubtypeAt(i).hashCode() == hashCode) {
satokfbedf1a2011-08-26 15:48:50 +0900479 tempHashCode = hashCode;
satokada8c4e2011-08-23 14:56:56 +0900480 break;
481 }
482 }
satokada8c4e2011-08-23 14:56:56 +0900483 final long ident = Binder.clearCallingIdentity();
484 try {
485 Settings.Secure.putString(mContext.getContentResolver(),
satokfbedf1a2011-08-26 15:48:50 +0900486 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(tempHashCode));
satokdf5659d2011-07-29 18:38:21 +0900487 } finally {
488 Binder.restoreCallingIdentity(ident);
489 }
satok988323c2011-06-22 16:38:13 +0900490 }
491
satoka33c4fc2011-08-25 16:50:11 +0900492 private void setSpellCheckerEnabledLocked(boolean enabled) {
493 if (DBG) {
494 Slog.w(TAG, "setSpellCheckerEnabled: " + enabled);
495 }
496 final long ident = Binder.clearCallingIdentity();
497 try {
498 Settings.Secure.putInt(mContext.getContentResolver(),
499 Settings.Secure.SPELL_CHECKER_ENABLED, enabled ? 1 : 0);
500 } finally {
501 Binder.restoreCallingIdentity(ident);
502 }
503 }
504
505 private boolean isSpellCheckerEnabledLocked() {
506 final long ident = Binder.clearCallingIdentity();
507 try {
508 final boolean retval = Settings.Secure.getInt(mContext.getContentResolver(),
509 Settings.Secure.SPELL_CHECKER_ENABLED, 1) == 1;
510 if (DBG) {
511 Slog.w(TAG, "getSpellCheckerEnabled: " + retval);
512 }
513 return retval;
514 } finally {
515 Binder.restoreCallingIdentity(ident);
516 }
517 }
518
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700519 @Override
520 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
521 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
522 != PackageManager.PERMISSION_GRANTED) {
523
524 pw.println("Permission Denial: can't dump TextServicesManagerService from from pid="
525 + Binder.getCallingPid()
526 + ", uid=" + Binder.getCallingUid());
527 return;
528 }
529
530 synchronized(mSpellCheckerMap) {
531 pw.println("Current Text Services Manager state:");
532 pw.println(" Spell Checker Map:");
533 for (Map.Entry<String, SpellCheckerInfo> ent : mSpellCheckerMap.entrySet()) {
534 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
535 SpellCheckerInfo info = ent.getValue();
536 pw.print(" "); pw.print("id="); pw.println(info.getId());
537 pw.print(" "); pw.print("comp=");
538 pw.println(info.getComponent().toShortString());
539 int NS = info.getSubtypeCount();
540 for (int i=0; i<NS; i++) {
541 SpellCheckerSubtype st = info.getSubtypeAt(i);
542 pw.print(" "); pw.print("Subtype #"); pw.print(i); pw.println(":");
543 pw.print(" "); pw.print("locale="); pw.println(st.getLocale());
544 pw.print(" "); pw.print("extraValue=");
545 pw.println(st.getExtraValue());
546 }
547 }
548 pw.println("");
549 pw.println(" Spell Checker Bind Groups:");
550 for (Map.Entry<String, SpellCheckerBindGroup> ent
551 : mSpellCheckerBindGroups.entrySet()) {
552 SpellCheckerBindGroup grp = ent.getValue();
553 pw.print(" "); pw.print(ent.getKey()); pw.print(" ");
554 pw.print(grp); pw.println(":");
555 pw.print(" "); pw.print("mInternalConnection=");
556 pw.println(grp.mInternalConnection);
557 pw.print(" "); pw.print("mSpellChecker=");
558 pw.println(grp.mSpellChecker);
559 pw.print(" "); pw.print("mBound="); pw.print(grp.mBound);
560 pw.print(" mConnected="); pw.println(grp.mConnected);
561 int NL = grp.mListeners.size();
562 for (int i=0; i<NL; i++) {
563 InternalDeathRecipient listener = grp.mListeners.get(i);
564 pw.print(" "); pw.print("Listener #"); pw.print(i); pw.println(":");
565 pw.print(" "); pw.print("mTsListener=");
566 pw.println(listener.mTsListener);
567 pw.print(" "); pw.print("mScListener=");
568 pw.println(listener.mScListener);
569 pw.print(" "); pw.print("mGroup=");
570 pw.println(listener.mGroup);
571 pw.print(" "); pw.print("mScLocale=");
572 pw.print(listener.mScLocale);
573 pw.print(" mUid="); pw.println(listener.mUid);
574 }
575 }
576 }
577 }
578
satok988323c2011-06-22 16:38:13 +0900579 // SpellCheckerBindGroup contains active text service session listeners.
580 // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from
581 // mSpellCheckerBindGroups
582 private class SpellCheckerBindGroup {
satokdf5659d2011-07-29 18:38:21 +0900583 private final String TAG = SpellCheckerBindGroup.class.getSimpleName();
satok6be6d752011-07-28 20:40:38 +0900584 private final InternalServiceConnection mInternalConnection;
585 private final ArrayList<InternalDeathRecipient> mListeners =
satok988323c2011-06-22 16:38:13 +0900586 new ArrayList<InternalDeathRecipient>();
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700587 public boolean mBound;
satok6be6d752011-07-28 20:40:38 +0900588 public ISpellCheckerService mSpellChecker;
589 public boolean mConnected;
satok988323c2011-06-22 16:38:13 +0900590
591 public SpellCheckerBindGroup(InternalServiceConnection connection,
592 ITextServicesSessionListener listener, String locale,
satok53578062011-08-03 16:08:59 +0900593 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900594 mInternalConnection = connection;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700595 mBound = true;
satok6be6d752011-07-28 20:40:38 +0900596 mConnected = false;
satok53578062011-08-03 16:08:59 +0900597 addListener(listener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900598 }
599
600 public void onServiceConnected(ISpellCheckerService spellChecker) {
satokda317ef2011-07-26 08:02:45 +0900601 if (DBG) {
602 Slog.d(TAG, "onServiceConnected");
603 }
satok988323c2011-06-22 16:38:13 +0900604 synchronized(mSpellCheckerMap) {
605 for (InternalDeathRecipient listener : mListeners) {
606 try {
607 final ISpellCheckerSession session = spellChecker.getISpellCheckerSession(
satok53578062011-08-03 16:08:59 +0900608 listener.mScLocale, listener.mScListener, listener.mBundle);
satok988323c2011-06-22 16:38:13 +0900609 listener.mTsListener.onServiceConnected(session);
610 } catch (RemoteException e) {
satokc7b60f722011-08-31 16:30:27 +0900611 Slog.e(TAG, "Exception in getting the spell checker session."
612 + "Reconnect to the spellchecker. ", e);
satok6be6d752011-07-28 20:40:38 +0900613 removeAll();
614 return;
satok988323c2011-06-22 16:38:13 +0900615 }
616 }
satok6be6d752011-07-28 20:40:38 +0900617 mSpellChecker = spellChecker;
618 mConnected = true;
satok988323c2011-06-22 16:38:13 +0900619 }
620 }
621
satok6be6d752011-07-28 20:40:38 +0900622 public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener,
satok53578062011-08-03 16:08:59 +0900623 String locale, ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satokda317ef2011-07-26 08:02:45 +0900624 if (DBG) {
625 Slog.d(TAG, "addListener: " + locale);
626 }
satok6be6d752011-07-28 20:40:38 +0900627 InternalDeathRecipient recipient = null;
satok988323c2011-06-22 16:38:13 +0900628 synchronized(mSpellCheckerMap) {
629 try {
630 final int size = mListeners.size();
631 for (int i = 0; i < size; ++i) {
632 if (mListeners.get(i).hasSpellCheckerListener(scListener)) {
633 // do not add the lister if the group already contains this.
satok6be6d752011-07-28 20:40:38 +0900634 return null;
satok988323c2011-06-22 16:38:13 +0900635 }
636 }
satok6be6d752011-07-28 20:40:38 +0900637 recipient = new InternalDeathRecipient(
satok53578062011-08-03 16:08:59 +0900638 this, tsListener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900639 scListener.asBinder().linkToDeath(recipient, 0);
satokdf5659d2011-07-29 18:38:21 +0900640 mListeners.add(recipient);
satok988323c2011-06-22 16:38:13 +0900641 } catch(RemoteException e) {
642 // do nothing
643 }
644 cleanLocked();
645 }
satok6be6d752011-07-28 20:40:38 +0900646 return recipient;
satok988323c2011-06-22 16:38:13 +0900647 }
648
649 public void removeListener(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900650 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900651 Slog.w(TAG, "remove listener: " + listener.hashCode());
satokda317ef2011-07-26 08:02:45 +0900652 }
satok988323c2011-06-22 16:38:13 +0900653 synchronized(mSpellCheckerMap) {
654 final int size = mListeners.size();
655 final ArrayList<InternalDeathRecipient> removeList =
656 new ArrayList<InternalDeathRecipient>();
657 for (int i = 0; i < size; ++i) {
658 final InternalDeathRecipient tempRecipient = mListeners.get(i);
659 if(tempRecipient.hasSpellCheckerListener(listener)) {
satokdf5659d2011-07-29 18:38:21 +0900660 if (DBG) {
661 Slog.w(TAG, "found existing listener.");
662 }
satok988323c2011-06-22 16:38:13 +0900663 removeList.add(tempRecipient);
664 }
665 }
666 final int removeSize = removeList.size();
667 for (int i = 0; i < removeSize; ++i) {
satokdf5659d2011-07-29 18:38:21 +0900668 if (DBG) {
669 Slog.w(TAG, "Remove " + removeList.get(i));
670 }
satok2520ed82011-10-31 19:38:05 +0900671 final InternalDeathRecipient idr = removeList.get(i);
672 idr.mScListener.asBinder().unlinkToDeath(idr, 0);
673 mListeners.remove(idr);
satok988323c2011-06-22 16:38:13 +0900674 }
675 cleanLocked();
676 }
677 }
678
satok4c3fa642011-11-30 18:17:59 +0900679 // cleanLocked may remove elements from mSpellCheckerBindGroups
satok988323c2011-06-22 16:38:13 +0900680 private void cleanLocked() {
satokda317ef2011-07-26 08:02:45 +0900681 if (DBG) {
682 Slog.d(TAG, "cleanLocked");
683 }
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700684 // If there are no more active listeners, clean up. Only do this
685 // once.
686 if (mBound && mListeners.isEmpty()) {
687 mBound = false;
satokc7b60f722011-08-31 16:30:27 +0900688 final String sciId = mInternalConnection.mSciId;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700689 SpellCheckerBindGroup cur = mSpellCheckerBindGroups.get(sciId);
690 if (cur == this) {
satokc7b60f722011-08-31 16:30:27 +0900691 if (DBG) {
692 Slog.d(TAG, "Remove bind group.");
693 }
694 mSpellCheckerBindGroups.remove(sciId);
satok6be6d752011-07-28 20:40:38 +0900695 }
satok988323c2011-06-22 16:38:13 +0900696 mContext.unbindService(mInternalConnection);
697 }
698 }
satok6be6d752011-07-28 20:40:38 +0900699
700 public void removeAll() {
701 Slog.e(TAG, "Remove the spell checker bind unexpectedly.");
satokdf5659d2011-07-29 18:38:21 +0900702 synchronized(mSpellCheckerMap) {
satok2520ed82011-10-31 19:38:05 +0900703 final int size = mListeners.size();
704 for (int i = 0; i < size; ++i) {
705 final InternalDeathRecipient idr = mListeners.get(i);
706 idr.mScListener.asBinder().unlinkToDeath(idr, 0);
707 }
satokdf5659d2011-07-29 18:38:21 +0900708 mListeners.clear();
709 cleanLocked();
710 }
satok6be6d752011-07-28 20:40:38 +0900711 }
satok988323c2011-06-22 16:38:13 +0900712 }
713
714 private class InternalServiceConnection implements ServiceConnection {
satok988323c2011-06-22 16:38:13 +0900715 private final String mSciId;
716 private final String mLocale;
satok53578062011-08-03 16:08:59 +0900717 private final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900718 public InternalServiceConnection(
satok060677f2011-11-17 09:40:56 +0900719 String id, String locale, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900720 mSciId = id;
721 mLocale = locale;
satok53578062011-08-03 16:08:59 +0900722 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900723 }
724
725 @Override
726 public void onServiceConnected(ComponentName name, IBinder service) {
727 synchronized(mSpellCheckerMap) {
satok6be6d752011-07-28 20:40:38 +0900728 if (DBG) {
729 Slog.w(TAG, "onServiceConnected: " + name);
730 }
satok988323c2011-06-22 16:38:13 +0900731 ISpellCheckerService spellChecker = ISpellCheckerService.Stub.asInterface(service);
732 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
Dianne Hackbornc7d233d2011-10-19 16:55:27 -0700733 if (group != null && this == group.mInternalConnection) {
satok988323c2011-06-22 16:38:13 +0900734 group.onServiceConnected(spellChecker);
735 }
736 }
737 }
738
739 @Override
740 public void onServiceDisconnected(ComponentName name) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700741 synchronized(mSpellCheckerMap) {
742 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
satok2cf1cf02011-10-21 15:25:23 +0900743 if (group != null && this == group.mInternalConnection) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700744 mSpellCheckerBindGroups.remove(mSciId);
745 }
746 }
satok988323c2011-06-22 16:38:13 +0900747 }
748 }
749
750 private class InternalDeathRecipient implements IBinder.DeathRecipient {
751 public final ITextServicesSessionListener mTsListener;
752 public final ISpellCheckerSessionListener mScListener;
753 public final String mScLocale;
754 private final SpellCheckerBindGroup mGroup;
satokdf5659d2011-07-29 18:38:21 +0900755 public final int mUid;
satok53578062011-08-03 16:08:59 +0900756 public final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900757 public InternalDeathRecipient(SpellCheckerBindGroup group,
758 ITextServicesSessionListener tsListener, String scLocale,
satok53578062011-08-03 16:08:59 +0900759 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900760 mTsListener = tsListener;
761 mScListener = scListener;
762 mScLocale = scLocale;
763 mGroup = group;
satokdf5659d2011-07-29 18:38:21 +0900764 mUid = uid;
satok53578062011-08-03 16:08:59 +0900765 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900766 }
767
768 public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) {
satokdf5659d2011-07-29 18:38:21 +0900769 return listener.asBinder().equals(mScListener.asBinder());
satok988323c2011-06-22 16:38:13 +0900770 }
771
772 @Override
773 public void binderDied() {
774 mGroup.removeListener(mScListener);
775 }
776 }
777}