blob: c74dd0038c5f8c7cc86a5ba5bc77a7472d2bb1f8 [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();
Dianne Hackbornd0d75032012-04-19 23:12:09 -070080 mMonitor.register(context, null, true);
satok988323c2011-06-22 16:38:13 +090081 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
Satoshi Kataoka17150cf2012-05-30 20:05:44 +0900201 // TODO: Save SpellCheckerSubtype by supported languages by looking at "locale".
satokada8c4e2011-08-23 14:56:56 +0900202 @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) {
Satoshi Kataoka17150cf2012-05-30 20:05:44 +0900253 final String scsLocale = scs.getLocale();
254 if (candidateLocale.equals(scsLocale)) {
satokb3879542011-08-26 17:35:27 +0900255 return scs;
256 } else if (candidate == null) {
satok7018a902012-05-24 18:10:37 +0900257 if (candidateLocale.length() >= 2 && scsLocale.length() >= 2
258 && candidateLocale.startsWith(scsLocale)) {
satok05f24702011-11-02 19:29:35 +0900259 // Fall back to the applicable language
satokb3879542011-08-26 17:35:27 +0900260 candidate = scs;
261 }
262 }
263 } else if (scs.hashCode() == hashCode) {
satoka33c4fc2011-08-25 16:50:11 +0900264 if (DBG) {
satok70deff42011-09-30 15:14:21 +0900265 Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
266 + ", " + scs.getLocale());
satoka33c4fc2011-08-25 16:50:11 +0900267 }
satok05f24702011-11-02 19:29:35 +0900268 // 3. Use the user specified spell check language
satokada8c4e2011-08-23 14:56:56 +0900269 return scs;
270 }
271 }
satok05f24702011-11-02 19:29:35 +0900272 // 4. Fall back to the applicable language and return it if not null
273 // 5. Simply just return it even if it's null which means we could find no suitable
274 // spell check languages
satokb3879542011-08-26 17:35:27 +0900275 return candidate;
satokada8c4e2011-08-23 14:56:56 +0900276 }
277 }
278
satok988323c2011-06-22 16:38:13 +0900279 @Override
satok5b9b5a92011-08-02 12:24:44 +0900280 public void getSpellCheckerService(String sciId, String locale,
satok53578062011-08-03 16:08:59 +0900281 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
282 Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900283 if (!mSystemReady) {
284 return;
285 }
satok5b9b5a92011-08-02 12:24:44 +0900286 if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) {
satok988323c2011-06-22 16:38:13 +0900287 Slog.e(TAG, "getSpellCheckerService: Invalid input.");
288 return;
289 }
satok988323c2011-06-22 16:38:13 +0900290 synchronized(mSpellCheckerMap) {
291 if (!mSpellCheckerMap.containsKey(sciId)) {
292 return;
293 }
satok5b9b5a92011-08-02 12:24:44 +0900294 final SpellCheckerInfo sci = mSpellCheckerMap.get(sciId);
satokdf5659d2011-07-29 18:38:21 +0900295 final int uid = Binder.getCallingUid();
satok988323c2011-06-22 16:38:13 +0900296 if (mSpellCheckerBindGroups.containsKey(sciId)) {
satok6be6d752011-07-28 20:40:38 +0900297 final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId);
298 if (bindGroup != null) {
299 final InternalDeathRecipient recipient =
300 mSpellCheckerBindGroups.get(sciId).addListener(
satok53578062011-08-03 16:08:59 +0900301 tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900302 if (recipient == null) {
303 if (DBG) {
304 Slog.w(TAG, "Didn't create a death recipient.");
305 }
306 return;
307 }
308 if (bindGroup.mSpellChecker == null & bindGroup.mConnected) {
309 Slog.e(TAG, "The state of the spell checker bind group is illegal.");
310 bindGroup.removeAll();
311 } else if (bindGroup.mSpellChecker != null) {
312 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900313 Slog.w(TAG, "Existing bind found. Return a spell checker session now. "
314 + "Listeners count = " + bindGroup.mListeners.size());
satok6be6d752011-07-28 20:40:38 +0900315 }
316 try {
317 final ISpellCheckerSession session =
318 bindGroup.mSpellChecker.getISpellCheckerSession(
satok53578062011-08-03 16:08:59 +0900319 recipient.mScLocale, recipient.mScListener, bundle);
satokdf5659d2011-07-29 18:38:21 +0900320 if (session != null) {
321 tsListener.onServiceConnected(session);
322 return;
323 } else {
324 if (DBG) {
325 Slog.w(TAG, "Existing bind already expired. ");
326 }
327 bindGroup.removeAll();
328 }
satok6be6d752011-07-28 20:40:38 +0900329 } catch (RemoteException e) {
330 Slog.e(TAG, "Exception in getting spell checker session: " + e);
331 bindGroup.removeAll();
332 }
333 }
334 }
satok988323c2011-06-22 16:38:13 +0900335 }
satok6be6d752011-07-28 20:40:38 +0900336 final long ident = Binder.clearCallingIdentity();
337 try {
satok53578062011-08-03 16:08:59 +0900338 startSpellCheckerServiceInnerLocked(
339 sci, locale, tsListener, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900340 } finally {
341 Binder.restoreCallingIdentity(ident);
satok988323c2011-06-22 16:38:13 +0900342 }
satok988323c2011-06-22 16:38:13 +0900343 }
344 return;
345 }
346
satoka33c4fc2011-08-25 16:50:11 +0900347 @Override
348 public boolean isSpellCheckerEnabled() {
349 synchronized(mSpellCheckerMap) {
350 return isSpellCheckerEnabledLocked();
351 }
352 }
353
satok6be6d752011-07-28 20:40:38 +0900354 private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale,
satokdf5659d2011-07-29 18:38:21 +0900355 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
satok53578062011-08-03 16:08:59 +0900356 int uid, Bundle bundle) {
satokdf5659d2011-07-29 18:38:21 +0900357 if (DBG) {
358 Slog.w(TAG, "Start spell checker session inner locked.");
359 }
satok6be6d752011-07-28 20:40:38 +0900360 final String sciId = info.getId();
361 final InternalServiceConnection connection = new InternalServiceConnection(
satok060677f2011-11-17 09:40:56 +0900362 sciId, locale, bundle);
satok6be6d752011-07-28 20:40:38 +0900363 final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE);
364 serviceIntent.setComponent(info.getComponent());
365 if (DBG) {
366 Slog.w(TAG, "bind service: " + info.getId());
367 }
368 if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) {
369 Slog.e(TAG, "Failed to get a spell checker service.");
370 return;
371 }
372 final SpellCheckerBindGroup group = new SpellCheckerBindGroup(
satok53578062011-08-03 16:08:59 +0900373 connection, tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900374 mSpellCheckerBindGroups.put(sciId, group);
375 }
376
satok988323c2011-06-22 16:38:13 +0900377 @Override
satok562ab582011-07-25 10:12:21 +0900378 public SpellCheckerInfo[] getEnabledSpellCheckers() {
satokda317ef2011-07-26 08:02:45 +0900379 if (DBG) {
380 Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size());
381 for (int i = 0; i < mSpellCheckerList.size(); ++i) {
382 Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName());
383 }
384 }
satok562ab582011-07-25 10:12:21 +0900385 return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]);
386 }
387
388 @Override
satok988323c2011-06-22 16:38:13 +0900389 public void finishSpellCheckerService(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900390 if (DBG) {
391 Slog.d(TAG, "FinishSpellCheckerService");
392 }
satok988323c2011-06-22 16:38:13 +0900393 synchronized(mSpellCheckerMap) {
satok4c3fa642011-11-30 18:17:59 +0900394 final ArrayList<SpellCheckerBindGroup> removeList =
395 new ArrayList<SpellCheckerBindGroup>();
satok988323c2011-06-22 16:38:13 +0900396 for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) {
397 if (group == null) continue;
satok4c3fa642011-11-30 18:17:59 +0900398 // Use removeList to avoid modifying mSpellCheckerBindGroups in this loop.
399 removeList.add(group);
400 }
401 final int removeSize = removeList.size();
402 for (int i = 0; i < removeSize; ++i) {
403 removeList.get(i).removeListener(listener);
satok988323c2011-06-22 16:38:13 +0900404 }
405 }
406 }
407
satokdf5659d2011-07-29 18:38:21 +0900408 @Override
satokada8c4e2011-08-23 14:56:56 +0900409 public void setCurrentSpellChecker(String locale, String sciId) {
satokdf5659d2011-07-29 18:38:21 +0900410 synchronized(mSpellCheckerMap) {
411 if (mContext.checkCallingOrSelfPermission(
412 android.Manifest.permission.WRITE_SECURE_SETTINGS)
413 != PackageManager.PERMISSION_GRANTED) {
414 throw new SecurityException(
415 "Requires permission "
416 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
417 }
satok5b9b5a92011-08-02 12:24:44 +0900418 setCurrentSpellCheckerLocked(sciId);
satokdf5659d2011-07-29 18:38:21 +0900419 }
420 }
421
satokada8c4e2011-08-23 14:56:56 +0900422 @Override
423 public void setCurrentSpellCheckerSubtype(String locale, int hashCode) {
424 synchronized(mSpellCheckerMap) {
425 if (mContext.checkCallingOrSelfPermission(
426 android.Manifest.permission.WRITE_SECURE_SETTINGS)
427 != PackageManager.PERMISSION_GRANTED) {
428 throw new SecurityException(
429 "Requires permission "
430 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
431 }
satoka33c4fc2011-08-25 16:50:11 +0900432 setCurrentSpellCheckerSubtypeLocked(hashCode);
433 }
434 }
435
436 @Override
437 public void setSpellCheckerEnabled(boolean enabled) {
438 synchronized(mSpellCheckerMap) {
439 if (mContext.checkCallingOrSelfPermission(
440 android.Manifest.permission.WRITE_SECURE_SETTINGS)
441 != PackageManager.PERMISSION_GRANTED) {
442 throw new SecurityException(
443 "Requires permission "
444 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
445 }
446 setSpellCheckerEnabledLocked(enabled);
satokada8c4e2011-08-23 14:56:56 +0900447 }
448 }
449
satok5b9b5a92011-08-02 12:24:44 +0900450 private void setCurrentSpellCheckerLocked(String sciId) {
satok562ab582011-07-25 10:12:21 +0900451 if (DBG) {
satok5b9b5a92011-08-02 12:24:44 +0900452 Slog.w(TAG, "setCurrentSpellChecker: " + sciId);
satok562ab582011-07-25 10:12:21 +0900453 }
satok5b9b5a92011-08-02 12:24:44 +0900454 if (TextUtils.isEmpty(sciId) || !mSpellCheckerMap.containsKey(sciId)) return;
satokf39daef2011-08-26 19:54:27 +0900455 final SpellCheckerInfo currentSci = getCurrentSpellChecker(null);
456 if (currentSci != null && currentSci.getId().equals(sciId)) {
457 // Do nothing if the current spell checker is same as new spell checker.
458 return;
459 }
satokdf5659d2011-07-29 18:38:21 +0900460 final long ident = Binder.clearCallingIdentity();
461 try {
462 Settings.Secure.putString(mContext.getContentResolver(),
satokada8c4e2011-08-23 14:56:56 +0900463 Settings.Secure.SELECTED_SPELL_CHECKER, sciId);
satokf39daef2011-08-26 19:54:27 +0900464 setCurrentSpellCheckerSubtypeLocked(0);
satokada8c4e2011-08-23 14:56:56 +0900465 } finally {
466 Binder.restoreCallingIdentity(ident);
467 }
468 }
469
satoka33c4fc2011-08-25 16:50:11 +0900470 private void setCurrentSpellCheckerSubtypeLocked(int hashCode) {
satokada8c4e2011-08-23 14:56:56 +0900471 if (DBG) {
472 Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode);
473 }
474 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satokfbedf1a2011-08-26 15:48:50 +0900475 int tempHashCode = 0;
476 for (int i = 0; sci != null && i < sci.getSubtypeCount(); ++i) {
satokada8c4e2011-08-23 14:56:56 +0900477 if(sci.getSubtypeAt(i).hashCode() == hashCode) {
satokfbedf1a2011-08-26 15:48:50 +0900478 tempHashCode = hashCode;
satokada8c4e2011-08-23 14:56:56 +0900479 break;
480 }
481 }
satokada8c4e2011-08-23 14:56:56 +0900482 final long ident = Binder.clearCallingIdentity();
483 try {
484 Settings.Secure.putString(mContext.getContentResolver(),
satokfbedf1a2011-08-26 15:48:50 +0900485 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(tempHashCode));
satokdf5659d2011-07-29 18:38:21 +0900486 } finally {
487 Binder.restoreCallingIdentity(ident);
488 }
satok988323c2011-06-22 16:38:13 +0900489 }
490
satoka33c4fc2011-08-25 16:50:11 +0900491 private void setSpellCheckerEnabledLocked(boolean enabled) {
492 if (DBG) {
493 Slog.w(TAG, "setSpellCheckerEnabled: " + enabled);
494 }
495 final long ident = Binder.clearCallingIdentity();
496 try {
497 Settings.Secure.putInt(mContext.getContentResolver(),
498 Settings.Secure.SPELL_CHECKER_ENABLED, enabled ? 1 : 0);
499 } finally {
500 Binder.restoreCallingIdentity(ident);
501 }
502 }
503
504 private boolean isSpellCheckerEnabledLocked() {
505 final long ident = Binder.clearCallingIdentity();
506 try {
507 final boolean retval = Settings.Secure.getInt(mContext.getContentResolver(),
508 Settings.Secure.SPELL_CHECKER_ENABLED, 1) == 1;
509 if (DBG) {
510 Slog.w(TAG, "getSpellCheckerEnabled: " + retval);
511 }
512 return retval;
513 } finally {
514 Binder.restoreCallingIdentity(ident);
515 }
516 }
517
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700518 @Override
519 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
520 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
521 != PackageManager.PERMISSION_GRANTED) {
522
523 pw.println("Permission Denial: can't dump TextServicesManagerService from from pid="
524 + Binder.getCallingPid()
525 + ", uid=" + Binder.getCallingUid());
526 return;
527 }
528
529 synchronized(mSpellCheckerMap) {
530 pw.println("Current Text Services Manager state:");
531 pw.println(" Spell Checker Map:");
532 for (Map.Entry<String, SpellCheckerInfo> ent : mSpellCheckerMap.entrySet()) {
533 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
534 SpellCheckerInfo info = ent.getValue();
535 pw.print(" "); pw.print("id="); pw.println(info.getId());
536 pw.print(" "); pw.print("comp=");
537 pw.println(info.getComponent().toShortString());
538 int NS = info.getSubtypeCount();
539 for (int i=0; i<NS; i++) {
540 SpellCheckerSubtype st = info.getSubtypeAt(i);
541 pw.print(" "); pw.print("Subtype #"); pw.print(i); pw.println(":");
542 pw.print(" "); pw.print("locale="); pw.println(st.getLocale());
543 pw.print(" "); pw.print("extraValue=");
544 pw.println(st.getExtraValue());
545 }
546 }
547 pw.println("");
548 pw.println(" Spell Checker Bind Groups:");
549 for (Map.Entry<String, SpellCheckerBindGroup> ent
550 : mSpellCheckerBindGroups.entrySet()) {
551 SpellCheckerBindGroup grp = ent.getValue();
552 pw.print(" "); pw.print(ent.getKey()); pw.print(" ");
553 pw.print(grp); pw.println(":");
554 pw.print(" "); pw.print("mInternalConnection=");
555 pw.println(grp.mInternalConnection);
556 pw.print(" "); pw.print("mSpellChecker=");
557 pw.println(grp.mSpellChecker);
558 pw.print(" "); pw.print("mBound="); pw.print(grp.mBound);
559 pw.print(" mConnected="); pw.println(grp.mConnected);
560 int NL = grp.mListeners.size();
561 for (int i=0; i<NL; i++) {
562 InternalDeathRecipient listener = grp.mListeners.get(i);
563 pw.print(" "); pw.print("Listener #"); pw.print(i); pw.println(":");
564 pw.print(" "); pw.print("mTsListener=");
565 pw.println(listener.mTsListener);
566 pw.print(" "); pw.print("mScListener=");
567 pw.println(listener.mScListener);
568 pw.print(" "); pw.print("mGroup=");
569 pw.println(listener.mGroup);
570 pw.print(" "); pw.print("mScLocale=");
571 pw.print(listener.mScLocale);
572 pw.print(" mUid="); pw.println(listener.mUid);
573 }
574 }
575 }
576 }
577
satok988323c2011-06-22 16:38:13 +0900578 // SpellCheckerBindGroup contains active text service session listeners.
579 // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from
580 // mSpellCheckerBindGroups
581 private class SpellCheckerBindGroup {
satokdf5659d2011-07-29 18:38:21 +0900582 private final String TAG = SpellCheckerBindGroup.class.getSimpleName();
satok6be6d752011-07-28 20:40:38 +0900583 private final InternalServiceConnection mInternalConnection;
satok4e713f12012-02-28 16:51:15 +0900584 private final CopyOnWriteArrayList<InternalDeathRecipient> mListeners =
585 new CopyOnWriteArrayList<InternalDeathRecipient>();
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700586 public boolean mBound;
satok6be6d752011-07-28 20:40:38 +0900587 public ISpellCheckerService mSpellChecker;
588 public boolean mConnected;
satok988323c2011-06-22 16:38:13 +0900589
590 public SpellCheckerBindGroup(InternalServiceConnection connection,
591 ITextServicesSessionListener listener, String locale,
satok53578062011-08-03 16:08:59 +0900592 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900593 mInternalConnection = connection;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700594 mBound = true;
satok6be6d752011-07-28 20:40:38 +0900595 mConnected = false;
satok53578062011-08-03 16:08:59 +0900596 addListener(listener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900597 }
598
599 public void onServiceConnected(ISpellCheckerService spellChecker) {
satokda317ef2011-07-26 08:02:45 +0900600 if (DBG) {
601 Slog.d(TAG, "onServiceConnected");
602 }
satok4e713f12012-02-28 16:51:15 +0900603
604 for (InternalDeathRecipient listener : mListeners) {
605 try {
606 final ISpellCheckerSession session = spellChecker.getISpellCheckerSession(
607 listener.mScLocale, listener.mScListener, listener.mBundle);
608 synchronized(mSpellCheckerMap) {
609 if (mListeners.contains(listener)) {
610 listener.mTsListener.onServiceConnected(session);
611 }
satok988323c2011-06-22 16:38:13 +0900612 }
satok4e713f12012-02-28 16:51:15 +0900613 } catch (RemoteException e) {
614 Slog.e(TAG, "Exception in getting the spell checker session."
615 + "Reconnect to the spellchecker. ", e);
616 removeAll();
617 return;
satok988323c2011-06-22 16:38:13 +0900618 }
satok4e713f12012-02-28 16:51:15 +0900619 }
620 synchronized(mSpellCheckerMap) {
satok6be6d752011-07-28 20:40:38 +0900621 mSpellChecker = spellChecker;
622 mConnected = true;
satok988323c2011-06-22 16:38:13 +0900623 }
624 }
625
satok6be6d752011-07-28 20:40:38 +0900626 public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener,
satok53578062011-08-03 16:08:59 +0900627 String locale, ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satokda317ef2011-07-26 08:02:45 +0900628 if (DBG) {
629 Slog.d(TAG, "addListener: " + locale);
630 }
satok6be6d752011-07-28 20:40:38 +0900631 InternalDeathRecipient recipient = null;
satok988323c2011-06-22 16:38:13 +0900632 synchronized(mSpellCheckerMap) {
633 try {
634 final int size = mListeners.size();
635 for (int i = 0; i < size; ++i) {
636 if (mListeners.get(i).hasSpellCheckerListener(scListener)) {
637 // do not add the lister if the group already contains this.
satok6be6d752011-07-28 20:40:38 +0900638 return null;
satok988323c2011-06-22 16:38:13 +0900639 }
640 }
satok6be6d752011-07-28 20:40:38 +0900641 recipient = new InternalDeathRecipient(
satok53578062011-08-03 16:08:59 +0900642 this, tsListener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900643 scListener.asBinder().linkToDeath(recipient, 0);
satokdf5659d2011-07-29 18:38:21 +0900644 mListeners.add(recipient);
satok988323c2011-06-22 16:38:13 +0900645 } catch(RemoteException e) {
646 // do nothing
647 }
648 cleanLocked();
649 }
satok6be6d752011-07-28 20:40:38 +0900650 return recipient;
satok988323c2011-06-22 16:38:13 +0900651 }
652
653 public void removeListener(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900654 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900655 Slog.w(TAG, "remove listener: " + listener.hashCode());
satokda317ef2011-07-26 08:02:45 +0900656 }
satok988323c2011-06-22 16:38:13 +0900657 synchronized(mSpellCheckerMap) {
658 final int size = mListeners.size();
659 final ArrayList<InternalDeathRecipient> removeList =
660 new ArrayList<InternalDeathRecipient>();
661 for (int i = 0; i < size; ++i) {
662 final InternalDeathRecipient tempRecipient = mListeners.get(i);
663 if(tempRecipient.hasSpellCheckerListener(listener)) {
satokdf5659d2011-07-29 18:38:21 +0900664 if (DBG) {
665 Slog.w(TAG, "found existing listener.");
666 }
satok988323c2011-06-22 16:38:13 +0900667 removeList.add(tempRecipient);
668 }
669 }
670 final int removeSize = removeList.size();
671 for (int i = 0; i < removeSize; ++i) {
satokdf5659d2011-07-29 18:38:21 +0900672 if (DBG) {
673 Slog.w(TAG, "Remove " + removeList.get(i));
674 }
satok2520ed82011-10-31 19:38:05 +0900675 final InternalDeathRecipient idr = removeList.get(i);
676 idr.mScListener.asBinder().unlinkToDeath(idr, 0);
677 mListeners.remove(idr);
satok988323c2011-06-22 16:38:13 +0900678 }
679 cleanLocked();
680 }
681 }
682
satok4c3fa642011-11-30 18:17:59 +0900683 // cleanLocked may remove elements from mSpellCheckerBindGroups
satok988323c2011-06-22 16:38:13 +0900684 private void cleanLocked() {
satokda317ef2011-07-26 08:02:45 +0900685 if (DBG) {
686 Slog.d(TAG, "cleanLocked");
687 }
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700688 // If there are no more active listeners, clean up. Only do this
689 // once.
690 if (mBound && mListeners.isEmpty()) {
691 mBound = false;
satokc7b60f722011-08-31 16:30:27 +0900692 final String sciId = mInternalConnection.mSciId;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700693 SpellCheckerBindGroup cur = mSpellCheckerBindGroups.get(sciId);
694 if (cur == this) {
satokc7b60f722011-08-31 16:30:27 +0900695 if (DBG) {
696 Slog.d(TAG, "Remove bind group.");
697 }
698 mSpellCheckerBindGroups.remove(sciId);
satok6be6d752011-07-28 20:40:38 +0900699 }
satok988323c2011-06-22 16:38:13 +0900700 mContext.unbindService(mInternalConnection);
701 }
702 }
satok6be6d752011-07-28 20:40:38 +0900703
704 public void removeAll() {
705 Slog.e(TAG, "Remove the spell checker bind unexpectedly.");
satokdf5659d2011-07-29 18:38:21 +0900706 synchronized(mSpellCheckerMap) {
satok2520ed82011-10-31 19:38:05 +0900707 final int size = mListeners.size();
708 for (int i = 0; i < size; ++i) {
709 final InternalDeathRecipient idr = mListeners.get(i);
710 idr.mScListener.asBinder().unlinkToDeath(idr, 0);
711 }
satokdf5659d2011-07-29 18:38:21 +0900712 mListeners.clear();
713 cleanLocked();
714 }
satok6be6d752011-07-28 20:40:38 +0900715 }
satok988323c2011-06-22 16:38:13 +0900716 }
717
718 private class InternalServiceConnection implements ServiceConnection {
satok988323c2011-06-22 16:38:13 +0900719 private final String mSciId;
720 private final String mLocale;
satok53578062011-08-03 16:08:59 +0900721 private final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900722 public InternalServiceConnection(
satok060677f2011-11-17 09:40:56 +0900723 String id, String locale, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900724 mSciId = id;
725 mLocale = locale;
satok53578062011-08-03 16:08:59 +0900726 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900727 }
728
729 @Override
730 public void onServiceConnected(ComponentName name, IBinder service) {
731 synchronized(mSpellCheckerMap) {
satok6be6d752011-07-28 20:40:38 +0900732 if (DBG) {
733 Slog.w(TAG, "onServiceConnected: " + name);
734 }
satok988323c2011-06-22 16:38:13 +0900735 ISpellCheckerService spellChecker = ISpellCheckerService.Stub.asInterface(service);
736 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
Dianne Hackbornc7d233d2011-10-19 16:55:27 -0700737 if (group != null && this == group.mInternalConnection) {
satok988323c2011-06-22 16:38:13 +0900738 group.onServiceConnected(spellChecker);
739 }
740 }
741 }
742
743 @Override
744 public void onServiceDisconnected(ComponentName name) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700745 synchronized(mSpellCheckerMap) {
746 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
satok2cf1cf02011-10-21 15:25:23 +0900747 if (group != null && this == group.mInternalConnection) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700748 mSpellCheckerBindGroups.remove(mSciId);
749 }
750 }
satok988323c2011-06-22 16:38:13 +0900751 }
752 }
753
754 private class InternalDeathRecipient implements IBinder.DeathRecipient {
755 public final ITextServicesSessionListener mTsListener;
756 public final ISpellCheckerSessionListener mScListener;
757 public final String mScLocale;
758 private final SpellCheckerBindGroup mGroup;
satokdf5659d2011-07-29 18:38:21 +0900759 public final int mUid;
satok53578062011-08-03 16:08:59 +0900760 public final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900761 public InternalDeathRecipient(SpellCheckerBindGroup group,
762 ITextServicesSessionListener tsListener, String scLocale,
satok53578062011-08-03 16:08:59 +0900763 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900764 mTsListener = tsListener;
765 mScListener = scListener;
766 mScLocale = scLocale;
767 mGroup = group;
satokdf5659d2011-07-29 18:38:21 +0900768 mUid = uid;
satok53578062011-08-03 16:08:59 +0900769 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900770 }
771
772 public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) {
satokdf5659d2011-07-29 18:38:21 +0900773 return listener.asBinder().equals(mScListener.asBinder());
satok988323c2011-06-22 16:38:13 +0900774 }
775
776 @Override
777 public void binderDied() {
778 mGroup.removeListener(mScListener);
779 }
780 }
781}