blob: 106bb3e65119095bc047d53877f9e76a4a14fa65 [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;
satok4e713f12012-02-28 16:51:15 +090055import java.util.concurrent.CopyOnWriteArrayList;
satok988323c2011-06-22 16:38:13 +090056
57public 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();
80 mMonitor.register(context, true);
81 synchronized (mSpellCheckerMap) {
82 buildSpellCheckerMapLocked(context, mSpellCheckerList, mSpellCheckerMap);
83 }
satokdf5659d2011-07-29 18:38:21 +090084 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.
satok5b9b5a92011-08-02 12:24:44 +090091 setCurrentSpellCheckerLocked(sci.getId());
satokdf5659d2011-07-29 18:38:21 +090092 }
93 }
satok988323c2011-06-22 16:38:13 +090094 }
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);
satokda317ef2011-07-26 08:02:45 +0900103 if (sci == null) return;
satok988323c2011-06-22 16:38:13 +0900104 final String packageName = sci.getPackageName();
105 final int change = isPackageDisappearing(packageName);
satok5b9b5a92011-08-02 12:24:44 +0900106 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 }
satok988323c2011-06-22 16:38:13 +0900114 }
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);
satok03b2ea12011-08-03 17:36:14 +0900138 try {
139 final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri);
satok3cb5b392011-08-26 11:55:21 +0900140 if (sci.getSubtypeCount() <= 0) {
141 Slog.w(TAG, "Skipping text service " + compName
142 + ": it does not contain subtypes.");
143 continue;
144 }
satok03b2ea12011-08-03 17:36:14 +0900145 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 }
satok988323c2011-06-22 16:38:13 +0900152 }
satokda317ef2011-07-26 08:02:45 +0900153 if (DBG) {
154 Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size());
155 }
satok988323c2011-06-22 16:38:13 +0900156 }
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())) {
satokda317ef2011-07-26 08:02:45 +0900169 if (DBG) {
170 Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName());
171 }
satok988323c2011-06-22 16:38:13 +0900172 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) {
satoka33c4fc2011-08-25 16:50:11 +0900187 final String curSpellCheckerId =
satok988323c2011-06-22 16:38:13 +0900188 Settings.Secure.getString(mContext.getContentResolver(),
satokada8c4e2011-08-23 14:56:56 +0900189 Settings.Secure.SELECTED_SPELL_CHECKER);
satok562ab582011-07-25 10:12:21 +0900190 if (DBG) {
191 Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId);
192 }
satok988323c2011-06-22 16:38:13 +0900193 if (TextUtils.isEmpty(curSpellCheckerId)) {
satokdf5659d2011-07-29 18:38:21 +0900194 return null;
satok988323c2011-06-22 16:38:13 +0900195 }
196 return mSpellCheckerMap.get(curSpellCheckerId);
197 }
198 }
199
satok3cb5b392011-08-26 11:55:21 +0900200 // TODO: Respect allowImplicitlySelectedSubtype
satokada8c4e2011-08-23 14:56:56 +0900201 // TODO: Save SpellCheckerSubtype by supported languages.
202 @Override
satok3cb5b392011-08-26 11:55:21 +0900203 public SpellCheckerSubtype getCurrentSpellCheckerSubtype(
204 String locale, boolean allowImplicitlySelectedSubtype) {
satokada8c4e2011-08-23 14:56:56 +0900205 synchronized (mSpellCheckerMap) {
206 final String subtypeHashCodeStr =
207 Settings.Secure.getString(mContext.getContentResolver(),
208 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE);
209 if (DBG) {
satokc7b60f722011-08-31 16:30:27 +0900210 Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCodeStr);
satokada8c4e2011-08-23 14:56:56 +0900211 }
212 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satoka33c4fc2011-08-25 16:50:11 +0900213 if (sci == null || sci.getSubtypeCount() == 0) {
214 if (DBG) {
215 Slog.w(TAG, "Subtype not found.");
216 }
satokada8c4e2011-08-23 14:56:56 +0900217 return null;
218 }
satokb3879542011-08-26 17:35:27 +0900219 final int hashCode;
220 if (!TextUtils.isEmpty(subtypeHashCodeStr)) {
221 hashCode = Integer.valueOf(subtypeHashCodeStr);
222 } else {
223 hashCode = 0;
224 }
225 if (hashCode == 0 && !allowImplicitlySelectedSubtype) {
satok3cb5b392011-08-26 11:55:21 +0900226 return null;
satokada8c4e2011-08-23 14:56:56 +0900227 }
satok05f24702011-11-02 19:29:35 +0900228 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 }
satokb3879542011-08-26 17:35:27 +0900249 SpellCheckerSubtype candidate = null;
satokada8c4e2011-08-23 14:56:56 +0900250 for (int i = 0; i < sci.getSubtypeCount(); ++i) {
251 final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
satokb3879542011-08-26 17:35:27 +0900252 if (hashCode == 0) {
satok05f24702011-11-02 19:29:35 +0900253 if (candidateLocale.equals(locale)) {
satokb3879542011-08-26 17:35:27 +0900254 return scs;
255 } else if (candidate == null) {
256 final String scsLocale = scs.getLocale();
satok05f24702011-11-02 19:29:35 +0900257 if (candidateLocale.length() >= 2
satokb3879542011-08-26 17:35:27 +0900258 && scsLocale.length() >= 2
satok05f24702011-11-02 19:29:35 +0900259 && candidateLocale.substring(0, 2).equals(
satokb3879542011-08-26 17:35:27 +0900260 scsLocale.substring(0, 2))) {
satok05f24702011-11-02 19:29:35 +0900261 // Fall back to the applicable language
satokb3879542011-08-26 17:35:27 +0900262 candidate = scs;
263 }
264 }
265 } else if (scs.hashCode() == hashCode) {
satoka33c4fc2011-08-25 16:50:11 +0900266 if (DBG) {
satok70deff42011-09-30 15:14:21 +0900267 Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
268 + ", " + scs.getLocale());
satoka33c4fc2011-08-25 16:50:11 +0900269 }
satok05f24702011-11-02 19:29:35 +0900270 // 3. Use the user specified spell check language
satokada8c4e2011-08-23 14:56:56 +0900271 return scs;
272 }
273 }
satok05f24702011-11-02 19:29:35 +0900274 // 4. Fall back to the applicable language and return it if not null
275 // 5. Simply just return it even if it's null which means we could find no suitable
276 // spell check languages
satokb3879542011-08-26 17:35:27 +0900277 return candidate;
satokada8c4e2011-08-23 14:56:56 +0900278 }
279 }
280
satok988323c2011-06-22 16:38:13 +0900281 @Override
satok5b9b5a92011-08-02 12:24:44 +0900282 public void getSpellCheckerService(String sciId, String locale,
satok53578062011-08-03 16:08:59 +0900283 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
284 Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900285 if (!mSystemReady) {
286 return;
287 }
satok5b9b5a92011-08-02 12:24:44 +0900288 if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) {
satok988323c2011-06-22 16:38:13 +0900289 Slog.e(TAG, "getSpellCheckerService: Invalid input.");
290 return;
291 }
satok988323c2011-06-22 16:38:13 +0900292 synchronized(mSpellCheckerMap) {
293 if (!mSpellCheckerMap.containsKey(sciId)) {
294 return;
295 }
satok5b9b5a92011-08-02 12:24:44 +0900296 final SpellCheckerInfo sci = mSpellCheckerMap.get(sciId);
satokdf5659d2011-07-29 18:38:21 +0900297 final int uid = Binder.getCallingUid();
satok988323c2011-06-22 16:38:13 +0900298 if (mSpellCheckerBindGroups.containsKey(sciId)) {
satok6be6d752011-07-28 20:40:38 +0900299 final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId);
300 if (bindGroup != null) {
301 final InternalDeathRecipient recipient =
302 mSpellCheckerBindGroups.get(sciId).addListener(
satok53578062011-08-03 16:08:59 +0900303 tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900304 if (recipient == null) {
305 if (DBG) {
306 Slog.w(TAG, "Didn't create a death recipient.");
307 }
308 return;
309 }
310 if (bindGroup.mSpellChecker == null & bindGroup.mConnected) {
311 Slog.e(TAG, "The state of the spell checker bind group is illegal.");
312 bindGroup.removeAll();
313 } else if (bindGroup.mSpellChecker != null) {
314 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900315 Slog.w(TAG, "Existing bind found. Return a spell checker session now. "
316 + "Listeners count = " + bindGroup.mListeners.size());
satok6be6d752011-07-28 20:40:38 +0900317 }
318 try {
319 final ISpellCheckerSession session =
320 bindGroup.mSpellChecker.getISpellCheckerSession(
satok53578062011-08-03 16:08:59 +0900321 recipient.mScLocale, recipient.mScListener, bundle);
satokdf5659d2011-07-29 18:38:21 +0900322 if (session != null) {
323 tsListener.onServiceConnected(session);
324 return;
325 } else {
326 if (DBG) {
327 Slog.w(TAG, "Existing bind already expired. ");
328 }
329 bindGroup.removeAll();
330 }
satok6be6d752011-07-28 20:40:38 +0900331 } catch (RemoteException e) {
332 Slog.e(TAG, "Exception in getting spell checker session: " + e);
333 bindGroup.removeAll();
334 }
335 }
336 }
satok988323c2011-06-22 16:38:13 +0900337 }
satok6be6d752011-07-28 20:40:38 +0900338 final long ident = Binder.clearCallingIdentity();
339 try {
satok53578062011-08-03 16:08:59 +0900340 startSpellCheckerServiceInnerLocked(
341 sci, locale, tsListener, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900342 } finally {
343 Binder.restoreCallingIdentity(ident);
satok988323c2011-06-22 16:38:13 +0900344 }
satok988323c2011-06-22 16:38:13 +0900345 }
346 return;
347 }
348
satoka33c4fc2011-08-25 16:50:11 +0900349 @Override
350 public boolean isSpellCheckerEnabled() {
351 synchronized(mSpellCheckerMap) {
352 return isSpellCheckerEnabledLocked();
353 }
354 }
355
satok6be6d752011-07-28 20:40:38 +0900356 private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale,
satokdf5659d2011-07-29 18:38:21 +0900357 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
satok53578062011-08-03 16:08:59 +0900358 int uid, Bundle bundle) {
satokdf5659d2011-07-29 18:38:21 +0900359 if (DBG) {
360 Slog.w(TAG, "Start spell checker session inner locked.");
361 }
satok6be6d752011-07-28 20:40:38 +0900362 final String sciId = info.getId();
363 final InternalServiceConnection connection = new InternalServiceConnection(
satok060677f2011-11-17 09:40:56 +0900364 sciId, locale, bundle);
satok6be6d752011-07-28 20:40:38 +0900365 final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE);
366 serviceIntent.setComponent(info.getComponent());
367 if (DBG) {
368 Slog.w(TAG, "bind service: " + info.getId());
369 }
370 if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) {
371 Slog.e(TAG, "Failed to get a spell checker service.");
372 return;
373 }
374 final SpellCheckerBindGroup group = new SpellCheckerBindGroup(
satok53578062011-08-03 16:08:59 +0900375 connection, tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900376 mSpellCheckerBindGroups.put(sciId, group);
377 }
378
satok988323c2011-06-22 16:38:13 +0900379 @Override
satok562ab582011-07-25 10:12:21 +0900380 public SpellCheckerInfo[] getEnabledSpellCheckers() {
satokda317ef2011-07-26 08:02:45 +0900381 if (DBG) {
382 Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size());
383 for (int i = 0; i < mSpellCheckerList.size(); ++i) {
384 Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName());
385 }
386 }
satok562ab582011-07-25 10:12:21 +0900387 return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]);
388 }
389
390 @Override
satok988323c2011-06-22 16:38:13 +0900391 public void finishSpellCheckerService(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900392 if (DBG) {
393 Slog.d(TAG, "FinishSpellCheckerService");
394 }
satok988323c2011-06-22 16:38:13 +0900395 synchronized(mSpellCheckerMap) {
satok4c3fa642011-11-30 18:17:59 +0900396 final ArrayList<SpellCheckerBindGroup> removeList =
397 new ArrayList<SpellCheckerBindGroup>();
satok988323c2011-06-22 16:38:13 +0900398 for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) {
399 if (group == null) continue;
satok4c3fa642011-11-30 18:17:59 +0900400 // Use removeList to avoid modifying mSpellCheckerBindGroups in this loop.
401 removeList.add(group);
402 }
403 final int removeSize = removeList.size();
404 for (int i = 0; i < removeSize; ++i) {
405 removeList.get(i).removeListener(listener);
satok988323c2011-06-22 16:38:13 +0900406 }
407 }
408 }
409
satokdf5659d2011-07-29 18:38:21 +0900410 @Override
satokada8c4e2011-08-23 14:56:56 +0900411 public void setCurrentSpellChecker(String locale, String sciId) {
satokdf5659d2011-07-29 18:38:21 +0900412 synchronized(mSpellCheckerMap) {
413 if (mContext.checkCallingOrSelfPermission(
414 android.Manifest.permission.WRITE_SECURE_SETTINGS)
415 != PackageManager.PERMISSION_GRANTED) {
416 throw new SecurityException(
417 "Requires permission "
418 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
419 }
satok5b9b5a92011-08-02 12:24:44 +0900420 setCurrentSpellCheckerLocked(sciId);
satokdf5659d2011-07-29 18:38:21 +0900421 }
422 }
423
satokada8c4e2011-08-23 14:56:56 +0900424 @Override
425 public void setCurrentSpellCheckerSubtype(String locale, int hashCode) {
426 synchronized(mSpellCheckerMap) {
427 if (mContext.checkCallingOrSelfPermission(
428 android.Manifest.permission.WRITE_SECURE_SETTINGS)
429 != PackageManager.PERMISSION_GRANTED) {
430 throw new SecurityException(
431 "Requires permission "
432 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
433 }
satoka33c4fc2011-08-25 16:50:11 +0900434 setCurrentSpellCheckerSubtypeLocked(hashCode);
435 }
436 }
437
438 @Override
439 public void setSpellCheckerEnabled(boolean enabled) {
440 synchronized(mSpellCheckerMap) {
441 if (mContext.checkCallingOrSelfPermission(
442 android.Manifest.permission.WRITE_SECURE_SETTINGS)
443 != PackageManager.PERMISSION_GRANTED) {
444 throw new SecurityException(
445 "Requires permission "
446 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
447 }
448 setSpellCheckerEnabledLocked(enabled);
satokada8c4e2011-08-23 14:56:56 +0900449 }
450 }
451
satok5b9b5a92011-08-02 12:24:44 +0900452 private void setCurrentSpellCheckerLocked(String sciId) {
satok562ab582011-07-25 10:12:21 +0900453 if (DBG) {
satok5b9b5a92011-08-02 12:24:44 +0900454 Slog.w(TAG, "setCurrentSpellChecker: " + sciId);
satok562ab582011-07-25 10:12:21 +0900455 }
satok5b9b5a92011-08-02 12:24:44 +0900456 if (TextUtils.isEmpty(sciId) || !mSpellCheckerMap.containsKey(sciId)) return;
satokf39daef2011-08-26 19:54:27 +0900457 final SpellCheckerInfo currentSci = getCurrentSpellChecker(null);
458 if (currentSci != null && currentSci.getId().equals(sciId)) {
459 // Do nothing if the current spell checker is same as new spell checker.
460 return;
461 }
satokdf5659d2011-07-29 18:38:21 +0900462 final long ident = Binder.clearCallingIdentity();
463 try {
464 Settings.Secure.putString(mContext.getContentResolver(),
satokada8c4e2011-08-23 14:56:56 +0900465 Settings.Secure.SELECTED_SPELL_CHECKER, sciId);
satokf39daef2011-08-26 19:54:27 +0900466 setCurrentSpellCheckerSubtypeLocked(0);
satokada8c4e2011-08-23 14:56:56 +0900467 } finally {
468 Binder.restoreCallingIdentity(ident);
469 }
470 }
471
satoka33c4fc2011-08-25 16:50:11 +0900472 private void setCurrentSpellCheckerSubtypeLocked(int hashCode) {
satokada8c4e2011-08-23 14:56:56 +0900473 if (DBG) {
474 Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode);
475 }
476 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satokfbedf1a2011-08-26 15:48:50 +0900477 int tempHashCode = 0;
478 for (int i = 0; sci != null && i < sci.getSubtypeCount(); ++i) {
satokada8c4e2011-08-23 14:56:56 +0900479 if(sci.getSubtypeAt(i).hashCode() == hashCode) {
satokfbedf1a2011-08-26 15:48:50 +0900480 tempHashCode = hashCode;
satokada8c4e2011-08-23 14:56:56 +0900481 break;
482 }
483 }
satokada8c4e2011-08-23 14:56:56 +0900484 final long ident = Binder.clearCallingIdentity();
485 try {
486 Settings.Secure.putString(mContext.getContentResolver(),
satokfbedf1a2011-08-26 15:48:50 +0900487 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(tempHashCode));
satokdf5659d2011-07-29 18:38:21 +0900488 } finally {
489 Binder.restoreCallingIdentity(ident);
490 }
satok988323c2011-06-22 16:38:13 +0900491 }
492
satoka33c4fc2011-08-25 16:50:11 +0900493 private void setSpellCheckerEnabledLocked(boolean enabled) {
494 if (DBG) {
495 Slog.w(TAG, "setSpellCheckerEnabled: " + enabled);
496 }
497 final long ident = Binder.clearCallingIdentity();
498 try {
499 Settings.Secure.putInt(mContext.getContentResolver(),
500 Settings.Secure.SPELL_CHECKER_ENABLED, enabled ? 1 : 0);
501 } finally {
502 Binder.restoreCallingIdentity(ident);
503 }
504 }
505
506 private boolean isSpellCheckerEnabledLocked() {
507 final long ident = Binder.clearCallingIdentity();
508 try {
509 final boolean retval = Settings.Secure.getInt(mContext.getContentResolver(),
510 Settings.Secure.SPELL_CHECKER_ENABLED, 1) == 1;
511 if (DBG) {
512 Slog.w(TAG, "getSpellCheckerEnabled: " + retval);
513 }
514 return retval;
515 } finally {
516 Binder.restoreCallingIdentity(ident);
517 }
518 }
519
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700520 @Override
521 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
522 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
523 != PackageManager.PERMISSION_GRANTED) {
524
525 pw.println("Permission Denial: can't dump TextServicesManagerService from from pid="
526 + Binder.getCallingPid()
527 + ", uid=" + Binder.getCallingUid());
528 return;
529 }
530
531 synchronized(mSpellCheckerMap) {
532 pw.println("Current Text Services Manager state:");
533 pw.println(" Spell Checker Map:");
534 for (Map.Entry<String, SpellCheckerInfo> ent : mSpellCheckerMap.entrySet()) {
535 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
536 SpellCheckerInfo info = ent.getValue();
537 pw.print(" "); pw.print("id="); pw.println(info.getId());
538 pw.print(" "); pw.print("comp=");
539 pw.println(info.getComponent().toShortString());
540 int NS = info.getSubtypeCount();
541 for (int i=0; i<NS; i++) {
542 SpellCheckerSubtype st = info.getSubtypeAt(i);
543 pw.print(" "); pw.print("Subtype #"); pw.print(i); pw.println(":");
544 pw.print(" "); pw.print("locale="); pw.println(st.getLocale());
545 pw.print(" "); pw.print("extraValue=");
546 pw.println(st.getExtraValue());
547 }
548 }
549 pw.println("");
550 pw.println(" Spell Checker Bind Groups:");
551 for (Map.Entry<String, SpellCheckerBindGroup> ent
552 : mSpellCheckerBindGroups.entrySet()) {
553 SpellCheckerBindGroup grp = ent.getValue();
554 pw.print(" "); pw.print(ent.getKey()); pw.print(" ");
555 pw.print(grp); pw.println(":");
556 pw.print(" "); pw.print("mInternalConnection=");
557 pw.println(grp.mInternalConnection);
558 pw.print(" "); pw.print("mSpellChecker=");
559 pw.println(grp.mSpellChecker);
560 pw.print(" "); pw.print("mBound="); pw.print(grp.mBound);
561 pw.print(" mConnected="); pw.println(grp.mConnected);
562 int NL = grp.mListeners.size();
563 for (int i=0; i<NL; i++) {
564 InternalDeathRecipient listener = grp.mListeners.get(i);
565 pw.print(" "); pw.print("Listener #"); pw.print(i); pw.println(":");
566 pw.print(" "); pw.print("mTsListener=");
567 pw.println(listener.mTsListener);
568 pw.print(" "); pw.print("mScListener=");
569 pw.println(listener.mScListener);
570 pw.print(" "); pw.print("mGroup=");
571 pw.println(listener.mGroup);
572 pw.print(" "); pw.print("mScLocale=");
573 pw.print(listener.mScLocale);
574 pw.print(" mUid="); pw.println(listener.mUid);
575 }
576 }
577 }
578 }
579
satok988323c2011-06-22 16:38:13 +0900580 // SpellCheckerBindGroup contains active text service session listeners.
581 // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from
582 // mSpellCheckerBindGroups
583 private class SpellCheckerBindGroup {
satokdf5659d2011-07-29 18:38:21 +0900584 private final String TAG = SpellCheckerBindGroup.class.getSimpleName();
satok6be6d752011-07-28 20:40:38 +0900585 private final InternalServiceConnection mInternalConnection;
satok4e713f12012-02-28 16:51:15 +0900586 private final CopyOnWriteArrayList<InternalDeathRecipient> mListeners =
587 new CopyOnWriteArrayList<InternalDeathRecipient>();
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700588 public boolean mBound;
satok6be6d752011-07-28 20:40:38 +0900589 public ISpellCheckerService mSpellChecker;
590 public boolean mConnected;
satok988323c2011-06-22 16:38:13 +0900591
592 public SpellCheckerBindGroup(InternalServiceConnection connection,
593 ITextServicesSessionListener listener, String locale,
satok53578062011-08-03 16:08:59 +0900594 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900595 mInternalConnection = connection;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700596 mBound = true;
satok6be6d752011-07-28 20:40:38 +0900597 mConnected = false;
satok53578062011-08-03 16:08:59 +0900598 addListener(listener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900599 }
600
601 public void onServiceConnected(ISpellCheckerService spellChecker) {
satokda317ef2011-07-26 08:02:45 +0900602 if (DBG) {
603 Slog.d(TAG, "onServiceConnected");
604 }
satok4e713f12012-02-28 16:51:15 +0900605
606 for (InternalDeathRecipient listener : mListeners) {
607 try {
608 final ISpellCheckerSession session = spellChecker.getISpellCheckerSession(
609 listener.mScLocale, listener.mScListener, listener.mBundle);
610 synchronized(mSpellCheckerMap) {
611 if (mListeners.contains(listener)) {
612 listener.mTsListener.onServiceConnected(session);
613 }
satok988323c2011-06-22 16:38:13 +0900614 }
satok4e713f12012-02-28 16:51:15 +0900615 } catch (RemoteException e) {
616 Slog.e(TAG, "Exception in getting the spell checker session."
617 + "Reconnect to the spellchecker. ", e);
618 removeAll();
619 return;
satok988323c2011-06-22 16:38:13 +0900620 }
satok4e713f12012-02-28 16:51:15 +0900621 }
622 synchronized(mSpellCheckerMap) {
satok6be6d752011-07-28 20:40:38 +0900623 mSpellChecker = spellChecker;
624 mConnected = true;
satok988323c2011-06-22 16:38:13 +0900625 }
626 }
627
satok6be6d752011-07-28 20:40:38 +0900628 public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener,
satok53578062011-08-03 16:08:59 +0900629 String locale, ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satokda317ef2011-07-26 08:02:45 +0900630 if (DBG) {
631 Slog.d(TAG, "addListener: " + locale);
632 }
satok6be6d752011-07-28 20:40:38 +0900633 InternalDeathRecipient recipient = null;
satok988323c2011-06-22 16:38:13 +0900634 synchronized(mSpellCheckerMap) {
635 try {
636 final int size = mListeners.size();
637 for (int i = 0; i < size; ++i) {
638 if (mListeners.get(i).hasSpellCheckerListener(scListener)) {
639 // do not add the lister if the group already contains this.
satok6be6d752011-07-28 20:40:38 +0900640 return null;
satok988323c2011-06-22 16:38:13 +0900641 }
642 }
satok6be6d752011-07-28 20:40:38 +0900643 recipient = new InternalDeathRecipient(
satok53578062011-08-03 16:08:59 +0900644 this, tsListener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900645 scListener.asBinder().linkToDeath(recipient, 0);
satokdf5659d2011-07-29 18:38:21 +0900646 mListeners.add(recipient);
satok988323c2011-06-22 16:38:13 +0900647 } catch(RemoteException e) {
648 // do nothing
649 }
650 cleanLocked();
651 }
satok6be6d752011-07-28 20:40:38 +0900652 return recipient;
satok988323c2011-06-22 16:38:13 +0900653 }
654
655 public void removeListener(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900656 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900657 Slog.w(TAG, "remove listener: " + listener.hashCode());
satokda317ef2011-07-26 08:02:45 +0900658 }
satok988323c2011-06-22 16:38:13 +0900659 synchronized(mSpellCheckerMap) {
660 final int size = mListeners.size();
661 final ArrayList<InternalDeathRecipient> removeList =
662 new ArrayList<InternalDeathRecipient>();
663 for (int i = 0; i < size; ++i) {
664 final InternalDeathRecipient tempRecipient = mListeners.get(i);
665 if(tempRecipient.hasSpellCheckerListener(listener)) {
satokdf5659d2011-07-29 18:38:21 +0900666 if (DBG) {
667 Slog.w(TAG, "found existing listener.");
668 }
satok988323c2011-06-22 16:38:13 +0900669 removeList.add(tempRecipient);
670 }
671 }
672 final int removeSize = removeList.size();
673 for (int i = 0; i < removeSize; ++i) {
satokdf5659d2011-07-29 18:38:21 +0900674 if (DBG) {
675 Slog.w(TAG, "Remove " + removeList.get(i));
676 }
satok2520ed82011-10-31 19:38:05 +0900677 final InternalDeathRecipient idr = removeList.get(i);
678 idr.mScListener.asBinder().unlinkToDeath(idr, 0);
679 mListeners.remove(idr);
satok988323c2011-06-22 16:38:13 +0900680 }
681 cleanLocked();
682 }
683 }
684
satok4c3fa642011-11-30 18:17:59 +0900685 // cleanLocked may remove elements from mSpellCheckerBindGroups
satok988323c2011-06-22 16:38:13 +0900686 private void cleanLocked() {
satokda317ef2011-07-26 08:02:45 +0900687 if (DBG) {
688 Slog.d(TAG, "cleanLocked");
689 }
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700690 // If there are no more active listeners, clean up. Only do this
691 // once.
692 if (mBound && mListeners.isEmpty()) {
693 mBound = false;
satokc7b60f722011-08-31 16:30:27 +0900694 final String sciId = mInternalConnection.mSciId;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700695 SpellCheckerBindGroup cur = mSpellCheckerBindGroups.get(sciId);
696 if (cur == this) {
satokc7b60f722011-08-31 16:30:27 +0900697 if (DBG) {
698 Slog.d(TAG, "Remove bind group.");
699 }
700 mSpellCheckerBindGroups.remove(sciId);
satok6be6d752011-07-28 20:40:38 +0900701 }
satok988323c2011-06-22 16:38:13 +0900702 mContext.unbindService(mInternalConnection);
703 }
704 }
satok6be6d752011-07-28 20:40:38 +0900705
706 public void removeAll() {
707 Slog.e(TAG, "Remove the spell checker bind unexpectedly.");
satokdf5659d2011-07-29 18:38:21 +0900708 synchronized(mSpellCheckerMap) {
satok2520ed82011-10-31 19:38:05 +0900709 final int size = mListeners.size();
710 for (int i = 0; i < size; ++i) {
711 final InternalDeathRecipient idr = mListeners.get(i);
712 idr.mScListener.asBinder().unlinkToDeath(idr, 0);
713 }
satokdf5659d2011-07-29 18:38:21 +0900714 mListeners.clear();
715 cleanLocked();
716 }
satok6be6d752011-07-28 20:40:38 +0900717 }
satok988323c2011-06-22 16:38:13 +0900718 }
719
720 private class InternalServiceConnection implements ServiceConnection {
satok988323c2011-06-22 16:38:13 +0900721 private final String mSciId;
722 private final String mLocale;
satok53578062011-08-03 16:08:59 +0900723 private final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900724 public InternalServiceConnection(
satok060677f2011-11-17 09:40:56 +0900725 String id, String locale, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900726 mSciId = id;
727 mLocale = locale;
satok53578062011-08-03 16:08:59 +0900728 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900729 }
730
731 @Override
732 public void onServiceConnected(ComponentName name, IBinder service) {
733 synchronized(mSpellCheckerMap) {
satok6be6d752011-07-28 20:40:38 +0900734 if (DBG) {
735 Slog.w(TAG, "onServiceConnected: " + name);
736 }
satok988323c2011-06-22 16:38:13 +0900737 ISpellCheckerService spellChecker = ISpellCheckerService.Stub.asInterface(service);
738 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
Dianne Hackbornc7d233d2011-10-19 16:55:27 -0700739 if (group != null && this == group.mInternalConnection) {
satok988323c2011-06-22 16:38:13 +0900740 group.onServiceConnected(spellChecker);
741 }
742 }
743 }
744
745 @Override
746 public void onServiceDisconnected(ComponentName name) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700747 synchronized(mSpellCheckerMap) {
748 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
satok2cf1cf02011-10-21 15:25:23 +0900749 if (group != null && this == group.mInternalConnection) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700750 mSpellCheckerBindGroups.remove(mSciId);
751 }
752 }
satok988323c2011-06-22 16:38:13 +0900753 }
754 }
755
756 private class InternalDeathRecipient implements IBinder.DeathRecipient {
757 public final ITextServicesSessionListener mTsListener;
758 public final ISpellCheckerSessionListener mScListener;
759 public final String mScLocale;
760 private final SpellCheckerBindGroup mGroup;
satokdf5659d2011-07-29 18:38:21 +0900761 public final int mUid;
satok53578062011-08-03 16:08:59 +0900762 public final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900763 public InternalDeathRecipient(SpellCheckerBindGroup group,
764 ITextServicesSessionListener tsListener, String scLocale,
satok53578062011-08-03 16:08:59 +0900765 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900766 mTsListener = tsListener;
767 mScListener = scListener;
768 mScLocale = scLocale;
769 mGroup = group;
satokdf5659d2011-07-29 18:38:21 +0900770 mUid = uid;
satok53578062011-08-03 16:08:59 +0900771 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900772 }
773
774 public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) {
satokdf5659d2011-07-29 18:38:21 +0900775 return listener.asBinder().equals(mScListener.asBinder());
satok988323c2011-06-22 16:38:13 +0900776 }
777
778 @Override
779 public void binderDied() {
780 mGroup.removeListener(mScListener);
781 }
782 }
783}