blob: 788ecda647c0971259c6254aab961da8a3a45f92 [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;
43import android.view.textservice.SpellCheckerInfo;
satokada8c4e2011-08-23 14:56:56 +090044import android.view.textservice.SpellCheckerSubtype;
satok988323c2011-06-22 16:38:13 +090045
Dianne Hackborn71e14da2011-10-16 16:28:10 -070046import java.io.FileDescriptor;
satok03b2ea12011-08-03 17:36:14 +090047import java.io.IOException;
Dianne Hackborn71e14da2011-10-16 16:28:10 -070048import java.io.PrintWriter;
satok988323c2011-06-22 16:38:13 +090049import java.util.ArrayList;
50import java.util.HashMap;
satok988323c2011-06-22 16:38:13 +090051import java.util.List;
Dianne Hackborn71e14da2011-10-16 16:28:10 -070052import java.util.Map;
satok988323c2011-06-22 16:38:13 +090053
54public class TextServicesManagerService extends ITextServicesManager.Stub {
55 private static final String TAG = TextServicesManagerService.class.getSimpleName();
56 private static final boolean DBG = false;
57
58 private final Context mContext;
59 private boolean mSystemReady;
60 private final TextServicesMonitor mMonitor;
61 private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap =
62 new HashMap<String, SpellCheckerInfo>();
63 private final ArrayList<SpellCheckerInfo> mSpellCheckerList = new ArrayList<SpellCheckerInfo>();
64 private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups =
65 new HashMap<String, SpellCheckerBindGroup>();
66
67 public void systemReady() {
68 if (!mSystemReady) {
69 mSystemReady = true;
70 }
71 }
72
73 public TextServicesManagerService(Context context) {
74 mSystemReady = false;
75 mContext = context;
76 mMonitor = new TextServicesMonitor();
77 mMonitor.register(context, true);
78 synchronized (mSpellCheckerMap) {
79 buildSpellCheckerMapLocked(context, mSpellCheckerList, mSpellCheckerMap);
80 }
satokdf5659d2011-07-29 18:38:21 +090081 SpellCheckerInfo sci = getCurrentSpellChecker(null);
82 if (sci == null) {
83 sci = findAvailSpellCheckerLocked(null, null);
84 if (sci != null) {
85 // Set the current spell checker if there is one or more spell checkers
86 // available. In this case, "sci" is the first one in the available spell
87 // checkers.
satok5b9b5a92011-08-02 12:24:44 +090088 setCurrentSpellCheckerLocked(sci.getId());
satokdf5659d2011-07-29 18:38:21 +090089 }
90 }
satok988323c2011-06-22 16:38:13 +090091 }
92
93 private class TextServicesMonitor extends PackageMonitor {
94 @Override
95 public void onSomePackagesChanged() {
96 synchronized (mSpellCheckerMap) {
97 buildSpellCheckerMapLocked(mContext, mSpellCheckerList, mSpellCheckerMap);
98 // TODO: Update for each locale
99 SpellCheckerInfo sci = getCurrentSpellChecker(null);
satokda317ef2011-07-26 08:02:45 +0900100 if (sci == null) return;
satok988323c2011-06-22 16:38:13 +0900101 final String packageName = sci.getPackageName();
102 final int change = isPackageDisappearing(packageName);
satok5b9b5a92011-08-02 12:24:44 +0900103 if (// Package disappearing
104 change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE
105 // Package modified
106 || isPackageModified(packageName)) {
107 sci = findAvailSpellCheckerLocked(null, packageName);
108 if (sci != null) {
109 setCurrentSpellCheckerLocked(sci.getId());
110 }
satok988323c2011-06-22 16:38:13 +0900111 }
112 }
113 }
114 }
115
116 private static void buildSpellCheckerMapLocked(Context context,
117 ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map) {
118 list.clear();
119 map.clear();
120 final PackageManager pm = context.getPackageManager();
121 List<ResolveInfo> services = pm.queryIntentServices(
122 new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA);
123 final int N = services.size();
124 for (int i = 0; i < N; ++i) {
125 final ResolveInfo ri = services.get(i);
126 final ServiceInfo si = ri.serviceInfo;
127 final ComponentName compName = new ComponentName(si.packageName, si.name);
128 if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) {
129 Slog.w(TAG, "Skipping text service " + compName
130 + ": it does not require the permission "
131 + android.Manifest.permission.BIND_TEXT_SERVICE);
132 continue;
133 }
134 if (DBG) Slog.d(TAG, "Add: " + compName);
satok03b2ea12011-08-03 17:36:14 +0900135 try {
136 final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri);
satok3cb5b392011-08-26 11:55:21 +0900137 if (sci.getSubtypeCount() <= 0) {
138 Slog.w(TAG, "Skipping text service " + compName
139 + ": it does not contain subtypes.");
140 continue;
141 }
satok03b2ea12011-08-03 17:36:14 +0900142 list.add(sci);
143 map.put(sci.getId(), sci);
144 } catch (XmlPullParserException e) {
145 Slog.w(TAG, "Unable to load the spell checker " + compName, e);
146 } catch (IOException e) {
147 Slog.w(TAG, "Unable to load the spell checker " + compName, e);
148 }
satok988323c2011-06-22 16:38:13 +0900149 }
satokda317ef2011-07-26 08:02:45 +0900150 if (DBG) {
151 Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size());
152 }
satok988323c2011-06-22 16:38:13 +0900153 }
154
155 // TODO: find an appropriate spell checker for specified locale
156 private SpellCheckerInfo findAvailSpellCheckerLocked(String locale, String prefPackage) {
157 final int spellCheckersCount = mSpellCheckerList.size();
158 if (spellCheckersCount == 0) {
159 Slog.w(TAG, "no available spell checker services found");
160 return null;
161 }
162 if (prefPackage != null) {
163 for (int i = 0; i < spellCheckersCount; ++i) {
164 final SpellCheckerInfo sci = mSpellCheckerList.get(i);
165 if (prefPackage.equals(sci.getPackageName())) {
satokda317ef2011-07-26 08:02:45 +0900166 if (DBG) {
167 Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName());
168 }
satok988323c2011-06-22 16:38:13 +0900169 return sci;
170 }
171 }
172 }
173 if (spellCheckersCount > 1) {
174 Slog.w(TAG, "more than one spell checker service found, picking first");
175 }
176 return mSpellCheckerList.get(0);
177 }
178
179 // TODO: Save SpellCheckerService by supported languages. Currently only one spell
180 // checker is saved.
181 @Override
182 public SpellCheckerInfo getCurrentSpellChecker(String locale) {
183 synchronized (mSpellCheckerMap) {
satoka33c4fc2011-08-25 16:50:11 +0900184 final String curSpellCheckerId =
satok988323c2011-06-22 16:38:13 +0900185 Settings.Secure.getString(mContext.getContentResolver(),
satokada8c4e2011-08-23 14:56:56 +0900186 Settings.Secure.SELECTED_SPELL_CHECKER);
satok562ab582011-07-25 10:12:21 +0900187 if (DBG) {
188 Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId);
189 }
satok988323c2011-06-22 16:38:13 +0900190 if (TextUtils.isEmpty(curSpellCheckerId)) {
satokdf5659d2011-07-29 18:38:21 +0900191 return null;
satok988323c2011-06-22 16:38:13 +0900192 }
193 return mSpellCheckerMap.get(curSpellCheckerId);
194 }
195 }
196
satok3cb5b392011-08-26 11:55:21 +0900197 // TODO: Respect allowImplicitlySelectedSubtype
satokada8c4e2011-08-23 14:56:56 +0900198 // TODO: Save SpellCheckerSubtype by supported languages.
199 @Override
satok3cb5b392011-08-26 11:55:21 +0900200 public SpellCheckerSubtype getCurrentSpellCheckerSubtype(
201 String locale, boolean allowImplicitlySelectedSubtype) {
satokada8c4e2011-08-23 14:56:56 +0900202 synchronized (mSpellCheckerMap) {
203 final String subtypeHashCodeStr =
204 Settings.Secure.getString(mContext.getContentResolver(),
205 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE);
206 if (DBG) {
satokc7b60f722011-08-31 16:30:27 +0900207 Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCodeStr);
satokada8c4e2011-08-23 14:56:56 +0900208 }
209 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satoka33c4fc2011-08-25 16:50:11 +0900210 if (sci == null || sci.getSubtypeCount() == 0) {
211 if (DBG) {
212 Slog.w(TAG, "Subtype not found.");
213 }
satokada8c4e2011-08-23 14:56:56 +0900214 return null;
215 }
satokb3879542011-08-26 17:35:27 +0900216 final int hashCode;
217 if (!TextUtils.isEmpty(subtypeHashCodeStr)) {
218 hashCode = Integer.valueOf(subtypeHashCodeStr);
219 } else {
220 hashCode = 0;
221 }
222 if (hashCode == 0 && !allowImplicitlySelectedSubtype) {
satok3cb5b392011-08-26 11:55:21 +0900223 return null;
satokada8c4e2011-08-23 14:56:56 +0900224 }
satokb3879542011-08-26 17:35:27 +0900225 final String systemLocale =
226 mContext.getResources().getConfiguration().locale.toString();
227 SpellCheckerSubtype candidate = null;
satokada8c4e2011-08-23 14:56:56 +0900228 for (int i = 0; i < sci.getSubtypeCount(); ++i) {
229 final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
satokb3879542011-08-26 17:35:27 +0900230 if (hashCode == 0) {
231 if (systemLocale.equals(locale)) {
232 return scs;
233 } else if (candidate == null) {
234 final String scsLocale = scs.getLocale();
235 if (systemLocale.length() >= 2
236 && scsLocale.length() >= 2
237 && systemLocale.substring(0, 2).equals(
238 scsLocale.substring(0, 2))) {
239 candidate = scs;
240 }
241 }
242 } else if (scs.hashCode() == hashCode) {
satoka33c4fc2011-08-25 16:50:11 +0900243 if (DBG) {
satok70deff42011-09-30 15:14:21 +0900244 Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
245 + ", " + scs.getLocale());
satoka33c4fc2011-08-25 16:50:11 +0900246 }
satokada8c4e2011-08-23 14:56:56 +0900247 return scs;
248 }
249 }
satokb3879542011-08-26 17:35:27 +0900250 return candidate;
satokada8c4e2011-08-23 14:56:56 +0900251 }
252 }
253
satok988323c2011-06-22 16:38:13 +0900254 @Override
satok5b9b5a92011-08-02 12:24:44 +0900255 public void getSpellCheckerService(String sciId, String locale,
satok53578062011-08-03 16:08:59 +0900256 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
257 Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900258 if (!mSystemReady) {
259 return;
260 }
satok5b9b5a92011-08-02 12:24:44 +0900261 if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) {
satok988323c2011-06-22 16:38:13 +0900262 Slog.e(TAG, "getSpellCheckerService: Invalid input.");
263 return;
264 }
satok988323c2011-06-22 16:38:13 +0900265 synchronized(mSpellCheckerMap) {
266 if (!mSpellCheckerMap.containsKey(sciId)) {
267 return;
268 }
satok5b9b5a92011-08-02 12:24:44 +0900269 final SpellCheckerInfo sci = mSpellCheckerMap.get(sciId);
satokdf5659d2011-07-29 18:38:21 +0900270 final int uid = Binder.getCallingUid();
satok988323c2011-06-22 16:38:13 +0900271 if (mSpellCheckerBindGroups.containsKey(sciId)) {
satok6be6d752011-07-28 20:40:38 +0900272 final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId);
273 if (bindGroup != null) {
274 final InternalDeathRecipient recipient =
275 mSpellCheckerBindGroups.get(sciId).addListener(
satok53578062011-08-03 16:08:59 +0900276 tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900277 if (recipient == null) {
278 if (DBG) {
279 Slog.w(TAG, "Didn't create a death recipient.");
280 }
281 return;
282 }
283 if (bindGroup.mSpellChecker == null & bindGroup.mConnected) {
284 Slog.e(TAG, "The state of the spell checker bind group is illegal.");
285 bindGroup.removeAll();
286 } else if (bindGroup.mSpellChecker != null) {
287 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900288 Slog.w(TAG, "Existing bind found. Return a spell checker session now. "
289 + "Listeners count = " + bindGroup.mListeners.size());
satok6be6d752011-07-28 20:40:38 +0900290 }
291 try {
292 final ISpellCheckerSession session =
293 bindGroup.mSpellChecker.getISpellCheckerSession(
satok53578062011-08-03 16:08:59 +0900294 recipient.mScLocale, recipient.mScListener, bundle);
satokdf5659d2011-07-29 18:38:21 +0900295 if (session != null) {
296 tsListener.onServiceConnected(session);
297 return;
298 } else {
299 if (DBG) {
300 Slog.w(TAG, "Existing bind already expired. ");
301 }
302 bindGroup.removeAll();
303 }
satok6be6d752011-07-28 20:40:38 +0900304 } catch (RemoteException e) {
305 Slog.e(TAG, "Exception in getting spell checker session: " + e);
306 bindGroup.removeAll();
307 }
308 }
309 }
satok988323c2011-06-22 16:38:13 +0900310 }
satok6be6d752011-07-28 20:40:38 +0900311 final long ident = Binder.clearCallingIdentity();
312 try {
satok53578062011-08-03 16:08:59 +0900313 startSpellCheckerServiceInnerLocked(
314 sci, locale, tsListener, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900315 } finally {
316 Binder.restoreCallingIdentity(ident);
satok988323c2011-06-22 16:38:13 +0900317 }
satok988323c2011-06-22 16:38:13 +0900318 }
319 return;
320 }
321
satoka33c4fc2011-08-25 16:50:11 +0900322 @Override
323 public boolean isSpellCheckerEnabled() {
324 synchronized(mSpellCheckerMap) {
325 return isSpellCheckerEnabledLocked();
326 }
327 }
328
satok6be6d752011-07-28 20:40:38 +0900329 private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale,
satokdf5659d2011-07-29 18:38:21 +0900330 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
satok53578062011-08-03 16:08:59 +0900331 int uid, Bundle bundle) {
satokdf5659d2011-07-29 18:38:21 +0900332 if (DBG) {
333 Slog.w(TAG, "Start spell checker session inner locked.");
334 }
satok6be6d752011-07-28 20:40:38 +0900335 final String sciId = info.getId();
336 final InternalServiceConnection connection = new InternalServiceConnection(
satok53578062011-08-03 16:08:59 +0900337 sciId, locale, scListener, bundle);
satok6be6d752011-07-28 20:40:38 +0900338 final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE);
339 serviceIntent.setComponent(info.getComponent());
340 if (DBG) {
341 Slog.w(TAG, "bind service: " + info.getId());
342 }
343 if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) {
344 Slog.e(TAG, "Failed to get a spell checker service.");
345 return;
346 }
347 final SpellCheckerBindGroup group = new SpellCheckerBindGroup(
satok53578062011-08-03 16:08:59 +0900348 connection, tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900349 mSpellCheckerBindGroups.put(sciId, group);
350 }
351
satok988323c2011-06-22 16:38:13 +0900352 @Override
satok562ab582011-07-25 10:12:21 +0900353 public SpellCheckerInfo[] getEnabledSpellCheckers() {
satokda317ef2011-07-26 08:02:45 +0900354 if (DBG) {
355 Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size());
356 for (int i = 0; i < mSpellCheckerList.size(); ++i) {
357 Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName());
358 }
359 }
satok562ab582011-07-25 10:12:21 +0900360 return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]);
361 }
362
363 @Override
satok988323c2011-06-22 16:38:13 +0900364 public void finishSpellCheckerService(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900365 if (DBG) {
366 Slog.d(TAG, "FinishSpellCheckerService");
367 }
satok988323c2011-06-22 16:38:13 +0900368 synchronized(mSpellCheckerMap) {
369 for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) {
370 if (group == null) continue;
371 group.removeListener(listener);
372 }
373 }
374 }
375
satokdf5659d2011-07-29 18:38:21 +0900376 @Override
satokada8c4e2011-08-23 14:56:56 +0900377 public void setCurrentSpellChecker(String locale, String sciId) {
satokdf5659d2011-07-29 18:38:21 +0900378 synchronized(mSpellCheckerMap) {
379 if (mContext.checkCallingOrSelfPermission(
380 android.Manifest.permission.WRITE_SECURE_SETTINGS)
381 != PackageManager.PERMISSION_GRANTED) {
382 throw new SecurityException(
383 "Requires permission "
384 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
385 }
satok5b9b5a92011-08-02 12:24:44 +0900386 setCurrentSpellCheckerLocked(sciId);
satokdf5659d2011-07-29 18:38:21 +0900387 }
388 }
389
satokada8c4e2011-08-23 14:56:56 +0900390 @Override
391 public void setCurrentSpellCheckerSubtype(String locale, int hashCode) {
392 synchronized(mSpellCheckerMap) {
393 if (mContext.checkCallingOrSelfPermission(
394 android.Manifest.permission.WRITE_SECURE_SETTINGS)
395 != PackageManager.PERMISSION_GRANTED) {
396 throw new SecurityException(
397 "Requires permission "
398 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
399 }
satoka33c4fc2011-08-25 16:50:11 +0900400 setCurrentSpellCheckerSubtypeLocked(hashCode);
401 }
402 }
403
404 @Override
405 public void setSpellCheckerEnabled(boolean enabled) {
406 synchronized(mSpellCheckerMap) {
407 if (mContext.checkCallingOrSelfPermission(
408 android.Manifest.permission.WRITE_SECURE_SETTINGS)
409 != PackageManager.PERMISSION_GRANTED) {
410 throw new SecurityException(
411 "Requires permission "
412 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
413 }
414 setSpellCheckerEnabledLocked(enabled);
satokada8c4e2011-08-23 14:56:56 +0900415 }
416 }
417
satok5b9b5a92011-08-02 12:24:44 +0900418 private void setCurrentSpellCheckerLocked(String sciId) {
satok562ab582011-07-25 10:12:21 +0900419 if (DBG) {
satok5b9b5a92011-08-02 12:24:44 +0900420 Slog.w(TAG, "setCurrentSpellChecker: " + sciId);
satok562ab582011-07-25 10:12:21 +0900421 }
satok5b9b5a92011-08-02 12:24:44 +0900422 if (TextUtils.isEmpty(sciId) || !mSpellCheckerMap.containsKey(sciId)) return;
satokf39daef2011-08-26 19:54:27 +0900423 final SpellCheckerInfo currentSci = getCurrentSpellChecker(null);
424 if (currentSci != null && currentSci.getId().equals(sciId)) {
425 // Do nothing if the current spell checker is same as new spell checker.
426 return;
427 }
satokdf5659d2011-07-29 18:38:21 +0900428 final long ident = Binder.clearCallingIdentity();
429 try {
430 Settings.Secure.putString(mContext.getContentResolver(),
satokada8c4e2011-08-23 14:56:56 +0900431 Settings.Secure.SELECTED_SPELL_CHECKER, sciId);
satokf39daef2011-08-26 19:54:27 +0900432 setCurrentSpellCheckerSubtypeLocked(0);
satokada8c4e2011-08-23 14:56:56 +0900433 } finally {
434 Binder.restoreCallingIdentity(ident);
435 }
436 }
437
satoka33c4fc2011-08-25 16:50:11 +0900438 private void setCurrentSpellCheckerSubtypeLocked(int hashCode) {
satokada8c4e2011-08-23 14:56:56 +0900439 if (DBG) {
440 Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode);
441 }
442 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satokfbedf1a2011-08-26 15:48:50 +0900443 int tempHashCode = 0;
444 for (int i = 0; sci != null && i < sci.getSubtypeCount(); ++i) {
satokada8c4e2011-08-23 14:56:56 +0900445 if(sci.getSubtypeAt(i).hashCode() == hashCode) {
satokfbedf1a2011-08-26 15:48:50 +0900446 tempHashCode = hashCode;
satokada8c4e2011-08-23 14:56:56 +0900447 break;
448 }
449 }
satokada8c4e2011-08-23 14:56:56 +0900450 final long ident = Binder.clearCallingIdentity();
451 try {
452 Settings.Secure.putString(mContext.getContentResolver(),
satokfbedf1a2011-08-26 15:48:50 +0900453 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(tempHashCode));
satokdf5659d2011-07-29 18:38:21 +0900454 } finally {
455 Binder.restoreCallingIdentity(ident);
456 }
satok988323c2011-06-22 16:38:13 +0900457 }
458
satoka33c4fc2011-08-25 16:50:11 +0900459 private void setSpellCheckerEnabledLocked(boolean enabled) {
460 if (DBG) {
461 Slog.w(TAG, "setSpellCheckerEnabled: " + enabled);
462 }
463 final long ident = Binder.clearCallingIdentity();
464 try {
465 Settings.Secure.putInt(mContext.getContentResolver(),
466 Settings.Secure.SPELL_CHECKER_ENABLED, enabled ? 1 : 0);
467 } finally {
468 Binder.restoreCallingIdentity(ident);
469 }
470 }
471
472 private boolean isSpellCheckerEnabledLocked() {
473 final long ident = Binder.clearCallingIdentity();
474 try {
475 final boolean retval = Settings.Secure.getInt(mContext.getContentResolver(),
476 Settings.Secure.SPELL_CHECKER_ENABLED, 1) == 1;
477 if (DBG) {
478 Slog.w(TAG, "getSpellCheckerEnabled: " + retval);
479 }
480 return retval;
481 } finally {
482 Binder.restoreCallingIdentity(ident);
483 }
484 }
485
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700486 @Override
487 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
488 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
489 != PackageManager.PERMISSION_GRANTED) {
490
491 pw.println("Permission Denial: can't dump TextServicesManagerService from from pid="
492 + Binder.getCallingPid()
493 + ", uid=" + Binder.getCallingUid());
494 return;
495 }
496
497 synchronized(mSpellCheckerMap) {
498 pw.println("Current Text Services Manager state:");
499 pw.println(" Spell Checker Map:");
500 for (Map.Entry<String, SpellCheckerInfo> ent : mSpellCheckerMap.entrySet()) {
501 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
502 SpellCheckerInfo info = ent.getValue();
503 pw.print(" "); pw.print("id="); pw.println(info.getId());
504 pw.print(" "); pw.print("comp=");
505 pw.println(info.getComponent().toShortString());
506 int NS = info.getSubtypeCount();
507 for (int i=0; i<NS; i++) {
508 SpellCheckerSubtype st = info.getSubtypeAt(i);
509 pw.print(" "); pw.print("Subtype #"); pw.print(i); pw.println(":");
510 pw.print(" "); pw.print("locale="); pw.println(st.getLocale());
511 pw.print(" "); pw.print("extraValue=");
512 pw.println(st.getExtraValue());
513 }
514 }
515 pw.println("");
516 pw.println(" Spell Checker Bind Groups:");
517 for (Map.Entry<String, SpellCheckerBindGroup> ent
518 : mSpellCheckerBindGroups.entrySet()) {
519 SpellCheckerBindGroup grp = ent.getValue();
520 pw.print(" "); pw.print(ent.getKey()); pw.print(" ");
521 pw.print(grp); pw.println(":");
522 pw.print(" "); pw.print("mInternalConnection=");
523 pw.println(grp.mInternalConnection);
524 pw.print(" "); pw.print("mSpellChecker=");
525 pw.println(grp.mSpellChecker);
526 pw.print(" "); pw.print("mBound="); pw.print(grp.mBound);
527 pw.print(" mConnected="); pw.println(grp.mConnected);
528 int NL = grp.mListeners.size();
529 for (int i=0; i<NL; i++) {
530 InternalDeathRecipient listener = grp.mListeners.get(i);
531 pw.print(" "); pw.print("Listener #"); pw.print(i); pw.println(":");
532 pw.print(" "); pw.print("mTsListener=");
533 pw.println(listener.mTsListener);
534 pw.print(" "); pw.print("mScListener=");
535 pw.println(listener.mScListener);
536 pw.print(" "); pw.print("mGroup=");
537 pw.println(listener.mGroup);
538 pw.print(" "); pw.print("mScLocale=");
539 pw.print(listener.mScLocale);
540 pw.print(" mUid="); pw.println(listener.mUid);
541 }
542 }
543 }
544 }
545
satok988323c2011-06-22 16:38:13 +0900546 // SpellCheckerBindGroup contains active text service session listeners.
547 // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from
548 // mSpellCheckerBindGroups
549 private class SpellCheckerBindGroup {
satokdf5659d2011-07-29 18:38:21 +0900550 private final String TAG = SpellCheckerBindGroup.class.getSimpleName();
satok6be6d752011-07-28 20:40:38 +0900551 private final InternalServiceConnection mInternalConnection;
552 private final ArrayList<InternalDeathRecipient> mListeners =
satok988323c2011-06-22 16:38:13 +0900553 new ArrayList<InternalDeathRecipient>();
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700554 public boolean mBound;
satok6be6d752011-07-28 20:40:38 +0900555 public ISpellCheckerService mSpellChecker;
556 public boolean mConnected;
satok988323c2011-06-22 16:38:13 +0900557
558 public SpellCheckerBindGroup(InternalServiceConnection connection,
559 ITextServicesSessionListener listener, String locale,
satok53578062011-08-03 16:08:59 +0900560 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900561 mInternalConnection = connection;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700562 mBound = true;
satok6be6d752011-07-28 20:40:38 +0900563 mConnected = false;
satok53578062011-08-03 16:08:59 +0900564 addListener(listener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900565 }
566
567 public void onServiceConnected(ISpellCheckerService spellChecker) {
satokda317ef2011-07-26 08:02:45 +0900568 if (DBG) {
569 Slog.d(TAG, "onServiceConnected");
570 }
satok988323c2011-06-22 16:38:13 +0900571 synchronized(mSpellCheckerMap) {
572 for (InternalDeathRecipient listener : mListeners) {
573 try {
574 final ISpellCheckerSession session = spellChecker.getISpellCheckerSession(
satok53578062011-08-03 16:08:59 +0900575 listener.mScLocale, listener.mScListener, listener.mBundle);
satok988323c2011-06-22 16:38:13 +0900576 listener.mTsListener.onServiceConnected(session);
577 } catch (RemoteException e) {
satokc7b60f722011-08-31 16:30:27 +0900578 Slog.e(TAG, "Exception in getting the spell checker session."
579 + "Reconnect to the spellchecker. ", e);
satok6be6d752011-07-28 20:40:38 +0900580 removeAll();
581 return;
satok988323c2011-06-22 16:38:13 +0900582 }
583 }
satok6be6d752011-07-28 20:40:38 +0900584 mSpellChecker = spellChecker;
585 mConnected = true;
satok988323c2011-06-22 16:38:13 +0900586 }
587 }
588
satok6be6d752011-07-28 20:40:38 +0900589 public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener,
satok53578062011-08-03 16:08:59 +0900590 String locale, ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satokda317ef2011-07-26 08:02:45 +0900591 if (DBG) {
592 Slog.d(TAG, "addListener: " + locale);
593 }
satok6be6d752011-07-28 20:40:38 +0900594 InternalDeathRecipient recipient = null;
satok988323c2011-06-22 16:38:13 +0900595 synchronized(mSpellCheckerMap) {
596 try {
597 final int size = mListeners.size();
598 for (int i = 0; i < size; ++i) {
599 if (mListeners.get(i).hasSpellCheckerListener(scListener)) {
600 // do not add the lister if the group already contains this.
satok6be6d752011-07-28 20:40:38 +0900601 return null;
satok988323c2011-06-22 16:38:13 +0900602 }
603 }
satok6be6d752011-07-28 20:40:38 +0900604 recipient = new InternalDeathRecipient(
satok53578062011-08-03 16:08:59 +0900605 this, tsListener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900606 scListener.asBinder().linkToDeath(recipient, 0);
satokdf5659d2011-07-29 18:38:21 +0900607 mListeners.add(recipient);
satok988323c2011-06-22 16:38:13 +0900608 } catch(RemoteException e) {
609 // do nothing
610 }
611 cleanLocked();
612 }
satok6be6d752011-07-28 20:40:38 +0900613 return recipient;
satok988323c2011-06-22 16:38:13 +0900614 }
615
616 public void removeListener(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900617 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900618 Slog.w(TAG, "remove listener: " + listener.hashCode());
satokda317ef2011-07-26 08:02:45 +0900619 }
satok988323c2011-06-22 16:38:13 +0900620 synchronized(mSpellCheckerMap) {
621 final int size = mListeners.size();
622 final ArrayList<InternalDeathRecipient> removeList =
623 new ArrayList<InternalDeathRecipient>();
624 for (int i = 0; i < size; ++i) {
625 final InternalDeathRecipient tempRecipient = mListeners.get(i);
626 if(tempRecipient.hasSpellCheckerListener(listener)) {
satokdf5659d2011-07-29 18:38:21 +0900627 if (DBG) {
628 Slog.w(TAG, "found existing listener.");
629 }
satok988323c2011-06-22 16:38:13 +0900630 removeList.add(tempRecipient);
631 }
632 }
633 final int removeSize = removeList.size();
634 for (int i = 0; i < removeSize; ++i) {
satokdf5659d2011-07-29 18:38:21 +0900635 if (DBG) {
636 Slog.w(TAG, "Remove " + removeList.get(i));
637 }
satok988323c2011-06-22 16:38:13 +0900638 mListeners.remove(removeList.get(i));
639 }
640 cleanLocked();
641 }
642 }
643
644 private void cleanLocked() {
satokda317ef2011-07-26 08:02:45 +0900645 if (DBG) {
646 Slog.d(TAG, "cleanLocked");
647 }
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700648 // If there are no more active listeners, clean up. Only do this
649 // once.
650 if (mBound && mListeners.isEmpty()) {
651 mBound = false;
satokc7b60f722011-08-31 16:30:27 +0900652 final String sciId = mInternalConnection.mSciId;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700653 SpellCheckerBindGroup cur = mSpellCheckerBindGroups.get(sciId);
654 if (cur == this) {
satokc7b60f722011-08-31 16:30:27 +0900655 if (DBG) {
656 Slog.d(TAG, "Remove bind group.");
657 }
658 mSpellCheckerBindGroups.remove(sciId);
satok6be6d752011-07-28 20:40:38 +0900659 }
satok988323c2011-06-22 16:38:13 +0900660 mContext.unbindService(mInternalConnection);
661 }
662 }
satok6be6d752011-07-28 20:40:38 +0900663
664 public void removeAll() {
665 Slog.e(TAG, "Remove the spell checker bind unexpectedly.");
satokdf5659d2011-07-29 18:38:21 +0900666 synchronized(mSpellCheckerMap) {
667 mListeners.clear();
668 cleanLocked();
669 }
satok6be6d752011-07-28 20:40:38 +0900670 }
satok988323c2011-06-22 16:38:13 +0900671 }
672
673 private class InternalServiceConnection implements ServiceConnection {
674 private final ISpellCheckerSessionListener mListener;
675 private final String mSciId;
676 private final String mLocale;
satok53578062011-08-03 16:08:59 +0900677 private final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900678 public InternalServiceConnection(
satok53578062011-08-03 16:08:59 +0900679 String id, String locale, ISpellCheckerSessionListener listener, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900680 mSciId = id;
681 mLocale = locale;
682 mListener = listener;
satok53578062011-08-03 16:08:59 +0900683 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900684 }
685
686 @Override
687 public void onServiceConnected(ComponentName name, IBinder service) {
688 synchronized(mSpellCheckerMap) {
satok6be6d752011-07-28 20:40:38 +0900689 if (DBG) {
690 Slog.w(TAG, "onServiceConnected: " + name);
691 }
satok988323c2011-06-22 16:38:13 +0900692 ISpellCheckerService spellChecker = ISpellCheckerService.Stub.asInterface(service);
693 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700694 if (this == group.mInternalConnection) {
satok988323c2011-06-22 16:38:13 +0900695 group.onServiceConnected(spellChecker);
696 }
697 }
698 }
699
700 @Override
701 public void onServiceDisconnected(ComponentName name) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700702 synchronized(mSpellCheckerMap) {
703 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
704 if (this == group.mInternalConnection) {
705 mSpellCheckerBindGroups.remove(mSciId);
706 }
707 }
satok988323c2011-06-22 16:38:13 +0900708 }
709 }
710
711 private class InternalDeathRecipient implements IBinder.DeathRecipient {
712 public final ITextServicesSessionListener mTsListener;
713 public final ISpellCheckerSessionListener mScListener;
714 public final String mScLocale;
715 private final SpellCheckerBindGroup mGroup;
satokdf5659d2011-07-29 18:38:21 +0900716 public final int mUid;
satok53578062011-08-03 16:08:59 +0900717 public final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900718 public InternalDeathRecipient(SpellCheckerBindGroup group,
719 ITextServicesSessionListener tsListener, String scLocale,
satok53578062011-08-03 16:08:59 +0900720 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900721 mTsListener = tsListener;
722 mScListener = scListener;
723 mScLocale = scLocale;
724 mGroup = group;
satokdf5659d2011-07-29 18:38:21 +0900725 mUid = uid;
satok53578062011-08-03 16:08:59 +0900726 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900727 }
728
729 public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) {
satokdf5659d2011-07-29 18:38:21 +0900730 return listener.asBinder().equals(mScListener.asBinder());
satok988323c2011-06-22 16:38:13 +0900731 }
732
733 @Override
734 public void binderDied() {
735 mGroup.removeListener(mScListener);
736 }
737 }
738}