blob: d4c436f6c8f5fbc15d28051867897eff5c89ca5c [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
Satoshi Kataoka00d2d412012-09-28 20:32:33 +090028import android.app.ActivityManagerNative;
29import android.app.AppGlobals;
30import android.app.IUserSwitchObserver;
satok988323c2011-06-22 16:38:13 +090031import android.content.ComponentName;
Satoshi Kataoka00d2d412012-09-28 20:32:33 +090032import android.content.ContentResolver;
satok988323c2011-06-22 16:38:13 +090033import android.content.Context;
34import android.content.Intent;
35import android.content.ServiceConnection;
36import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
38import android.content.pm.ServiceInfo;
satok6be6d752011-07-28 20:40:38 +090039import android.os.Binder;
satok53578062011-08-03 16:08:59 +090040import android.os.Bundle;
satok988323c2011-06-22 16:38:13 +090041import android.os.IBinder;
Satoshi Kataoka00d2d412012-09-28 20:32:33 +090042import android.os.IRemoteCallback;
43import android.os.Process;
satok988323c2011-06-22 16:38:13 +090044import android.os.RemoteException;
Satoshi Kataoka00d2d412012-09-28 20:32:33 +090045import android.os.UserHandle;
satok988323c2011-06-22 16:38:13 +090046import android.provider.Settings;
satok988323c2011-06-22 16:38:13 +090047import android.service.textservice.SpellCheckerService;
satok53578062011-08-03 16:08:59 +090048import android.text.TextUtils;
satok988323c2011-06-22 16:38:13 +090049import android.util.Slog;
satok05f24702011-11-02 19:29:35 +090050import android.view.inputmethod.InputMethodManager;
51import android.view.inputmethod.InputMethodSubtype;
satok988323c2011-06-22 16:38:13 +090052import android.view.textservice.SpellCheckerInfo;
satokada8c4e2011-08-23 14:56:56 +090053import android.view.textservice.SpellCheckerSubtype;
satok988323c2011-06-22 16:38:13 +090054
Dianne Hackborn71e14da2011-10-16 16:28:10 -070055import java.io.FileDescriptor;
satok03b2ea12011-08-03 17:36:14 +090056import java.io.IOException;
Dianne Hackborn71e14da2011-10-16 16:28:10 -070057import java.io.PrintWriter;
satok988323c2011-06-22 16:38:13 +090058import java.util.ArrayList;
59import java.util.HashMap;
satok988323c2011-06-22 16:38:13 +090060import java.util.List;
Dianne Hackborn71e14da2011-10-16 16:28:10 -070061import java.util.Map;
satok4e713f12012-02-28 16:51:15 +090062import java.util.concurrent.CopyOnWriteArrayList;
satok988323c2011-06-22 16:38:13 +090063
64public class TextServicesManagerService extends ITextServicesManager.Stub {
65 private static final String TAG = TextServicesManagerService.class.getSimpleName();
66 private static final boolean DBG = false;
67
68 private final Context mContext;
69 private boolean mSystemReady;
70 private final TextServicesMonitor mMonitor;
71 private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap =
72 new HashMap<String, SpellCheckerInfo>();
73 private final ArrayList<SpellCheckerInfo> mSpellCheckerList = new ArrayList<SpellCheckerInfo>();
74 private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups =
75 new HashMap<String, SpellCheckerBindGroup>();
Satoshi Kataoka00d2d412012-09-28 20:32:33 +090076 private final TextServicesSettings mSettings;
satok988323c2011-06-22 16:38:13 +090077
Svetoslav Ganova0027152013-06-25 14:59:53 -070078 public void systemRunning() {
satok988323c2011-06-22 16:38:13 +090079 if (!mSystemReady) {
80 mSystemReady = true;
81 }
82 }
83
84 public TextServicesManagerService(Context context) {
85 mSystemReady = false;
86 mContext = context;
Satoshi Kataoka00d2d412012-09-28 20:32:33 +090087 int userId = UserHandle.USER_OWNER;
88 try {
89 ActivityManagerNative.getDefault().registerUserSwitchObserver(
90 new IUserSwitchObserver.Stub() {
91 @Override
92 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
93 synchronized(mSpellCheckerMap) {
94 switchUserLocked(newUserId);
95 }
96 if (reply != null) {
97 try {
98 reply.sendResult(null);
99 } catch (RemoteException e) {
100 }
101 }
102 }
103
104 @Override
105 public void onUserSwitchComplete(int newUserId) throws RemoteException {
106 }
107 });
108 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
109 } catch (RemoteException e) {
110 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
111 }
satok988323c2011-06-22 16:38:13 +0900112 mMonitor = new TextServicesMonitor();
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700113 mMonitor.register(context, null, true);
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900114 mSettings = new TextServicesSettings(context.getContentResolver(), userId);
115
116 // "switchUserLocked" initializes the states for the foreground user
117 switchUserLocked(userId);
118 }
119
120 private void switchUserLocked(int userId) {
121 mSettings.setCurrentUserId(userId);
122 unbindServiceLocked();
123 buildSpellCheckerMapLocked(mContext, mSpellCheckerList, mSpellCheckerMap, mSettings);
satokdf5659d2011-07-29 18:38:21 +0900124 SpellCheckerInfo sci = getCurrentSpellChecker(null);
125 if (sci == null) {
126 sci = findAvailSpellCheckerLocked(null, null);
127 if (sci != null) {
128 // Set the current spell checker if there is one or more spell checkers
129 // available. In this case, "sci" is the first one in the available spell
130 // checkers.
satok5b9b5a92011-08-02 12:24:44 +0900131 setCurrentSpellCheckerLocked(sci.getId());
satokdf5659d2011-07-29 18:38:21 +0900132 }
133 }
satok988323c2011-06-22 16:38:13 +0900134 }
135
136 private class TextServicesMonitor extends PackageMonitor {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900137 private boolean isChangingPackagesOfCurrentUser() {
138 final int userId = getChangingUserId();
139 final boolean retval = userId == mSettings.getCurrentUserId();
140 if (DBG) {
141 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
142 }
143 return retval;
144 }
145
satok988323c2011-06-22 16:38:13 +0900146 @Override
147 public void onSomePackagesChanged() {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900148 if (!isChangingPackagesOfCurrentUser()) {
149 return;
150 }
satok988323c2011-06-22 16:38:13 +0900151 synchronized (mSpellCheckerMap) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900152 buildSpellCheckerMapLocked(
153 mContext, mSpellCheckerList, mSpellCheckerMap, mSettings);
satok988323c2011-06-22 16:38:13 +0900154 // TODO: Update for each locale
155 SpellCheckerInfo sci = getCurrentSpellChecker(null);
Satoshi Kataoka02260e22013-08-02 16:22:04 +0900156 // If no spell checker is enabled, just return. The user should explicitly
157 // enable the spell checker.
satokda317ef2011-07-26 08:02:45 +0900158 if (sci == null) return;
satok988323c2011-06-22 16:38:13 +0900159 final String packageName = sci.getPackageName();
160 final int change = isPackageDisappearing(packageName);
satok5b9b5a92011-08-02 12:24:44 +0900161 if (// Package disappearing
162 change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE
163 // Package modified
164 || isPackageModified(packageName)) {
165 sci = findAvailSpellCheckerLocked(null, packageName);
166 if (sci != null) {
167 setCurrentSpellCheckerLocked(sci.getId());
168 }
satok988323c2011-06-22 16:38:13 +0900169 }
170 }
171 }
172 }
173
174 private static void buildSpellCheckerMapLocked(Context context,
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900175 ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map,
176 TextServicesSettings settings) {
satok988323c2011-06-22 16:38:13 +0900177 list.clear();
178 map.clear();
179 final PackageManager pm = context.getPackageManager();
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900180 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
181 new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA,
182 settings.getCurrentUserId());
satok988323c2011-06-22 16:38:13 +0900183 final int N = services.size();
184 for (int i = 0; i < N; ++i) {
185 final ResolveInfo ri = services.get(i);
186 final ServiceInfo si = ri.serviceInfo;
187 final ComponentName compName = new ComponentName(si.packageName, si.name);
188 if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) {
189 Slog.w(TAG, "Skipping text service " + compName
190 + ": it does not require the permission "
191 + android.Manifest.permission.BIND_TEXT_SERVICE);
192 continue;
193 }
194 if (DBG) Slog.d(TAG, "Add: " + compName);
satok03b2ea12011-08-03 17:36:14 +0900195 try {
196 final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri);
satok3cb5b392011-08-26 11:55:21 +0900197 if (sci.getSubtypeCount() <= 0) {
198 Slog.w(TAG, "Skipping text service " + compName
199 + ": it does not contain subtypes.");
200 continue;
201 }
satok03b2ea12011-08-03 17:36:14 +0900202 list.add(sci);
203 map.put(sci.getId(), sci);
204 } catch (XmlPullParserException e) {
205 Slog.w(TAG, "Unable to load the spell checker " + compName, e);
206 } catch (IOException e) {
207 Slog.w(TAG, "Unable to load the spell checker " + compName, e);
208 }
satok988323c2011-06-22 16:38:13 +0900209 }
satokda317ef2011-07-26 08:02:45 +0900210 if (DBG) {
211 Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size());
212 }
satok988323c2011-06-22 16:38:13 +0900213 }
214
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900215 // ---------------------------------------------------------------------------------------
216 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
217 // 1) it comes from the system process
218 // 2) the calling process' user id is identical to the current user id TSMS thinks.
219 private boolean calledFromValidUser() {
220 final int uid = Binder.getCallingUid();
221 final int userId = UserHandle.getUserId(uid);
222 if (DBG) {
223 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
224 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
225 + " calling userId = " + userId + ", foreground user id = "
226 + mSettings.getCurrentUserId());
227 try {
228 final String[] packageNames = AppGlobals.getPackageManager().getPackagesForUid(uid);
229 for (int i = 0; i < packageNames.length; ++i) {
230 if (DBG) {
231 Slog.d(TAG, "--- process name for "+ uid + " = " + packageNames[i]);
232 }
233 }
234 } catch (RemoteException e) {
235 }
236 }
237
238 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
239 return true;
240 } else {
241 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
242 return false;
243 }
244 }
245
246 private boolean bindCurrentSpellCheckerService(
247 Intent service, ServiceConnection conn, int flags) {
248 if (service == null || conn == null) {
249 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
250 return false;
251 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800252 return mContext.bindServiceAsUser(service, conn, flags,
253 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900254 }
255
256 private void unbindServiceLocked() {
257 for (SpellCheckerBindGroup scbg : mSpellCheckerBindGroups.values()) {
258 scbg.removeAll();
259 }
260 mSpellCheckerBindGroups.clear();
261 }
262
satok988323c2011-06-22 16:38:13 +0900263 // TODO: find an appropriate spell checker for specified locale
264 private SpellCheckerInfo findAvailSpellCheckerLocked(String locale, String prefPackage) {
265 final int spellCheckersCount = mSpellCheckerList.size();
266 if (spellCheckersCount == 0) {
267 Slog.w(TAG, "no available spell checker services found");
268 return null;
269 }
270 if (prefPackage != null) {
271 for (int i = 0; i < spellCheckersCount; ++i) {
272 final SpellCheckerInfo sci = mSpellCheckerList.get(i);
273 if (prefPackage.equals(sci.getPackageName())) {
satokda317ef2011-07-26 08:02:45 +0900274 if (DBG) {
275 Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName());
276 }
satok988323c2011-06-22 16:38:13 +0900277 return sci;
278 }
279 }
280 }
281 if (spellCheckersCount > 1) {
282 Slog.w(TAG, "more than one spell checker service found, picking first");
283 }
284 return mSpellCheckerList.get(0);
285 }
286
287 // TODO: Save SpellCheckerService by supported languages. Currently only one spell
288 // checker is saved.
289 @Override
290 public SpellCheckerInfo getCurrentSpellChecker(String locale) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900291 // TODO: Make this work even for non-current users?
292 if (!calledFromValidUser()) {
293 return null;
294 }
satok988323c2011-06-22 16:38:13 +0900295 synchronized (mSpellCheckerMap) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900296 final String curSpellCheckerId = mSettings.getSelectedSpellChecker();
satok562ab582011-07-25 10:12:21 +0900297 if (DBG) {
298 Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId);
299 }
satok988323c2011-06-22 16:38:13 +0900300 if (TextUtils.isEmpty(curSpellCheckerId)) {
satokdf5659d2011-07-29 18:38:21 +0900301 return null;
satok988323c2011-06-22 16:38:13 +0900302 }
303 return mSpellCheckerMap.get(curSpellCheckerId);
304 }
305 }
306
satok3cb5b392011-08-26 11:55:21 +0900307 // TODO: Respect allowImplicitlySelectedSubtype
Satoshi Kataoka17150cf2012-05-30 20:05:44 +0900308 // TODO: Save SpellCheckerSubtype by supported languages by looking at "locale".
satokada8c4e2011-08-23 14:56:56 +0900309 @Override
satok3cb5b392011-08-26 11:55:21 +0900310 public SpellCheckerSubtype getCurrentSpellCheckerSubtype(
311 String locale, boolean allowImplicitlySelectedSubtype) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900312 // TODO: Make this work even for non-current users?
313 if (!calledFromValidUser()) {
314 return null;
315 }
satokada8c4e2011-08-23 14:56:56 +0900316 synchronized (mSpellCheckerMap) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900317 final String subtypeHashCodeStr = mSettings.getSelectedSpellCheckerSubtype();
satokada8c4e2011-08-23 14:56:56 +0900318 if (DBG) {
satokc7b60f722011-08-31 16:30:27 +0900319 Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCodeStr);
satokada8c4e2011-08-23 14:56:56 +0900320 }
321 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satoka33c4fc2011-08-25 16:50:11 +0900322 if (sci == null || sci.getSubtypeCount() == 0) {
323 if (DBG) {
324 Slog.w(TAG, "Subtype not found.");
325 }
satokada8c4e2011-08-23 14:56:56 +0900326 return null;
327 }
satokb3879542011-08-26 17:35:27 +0900328 final int hashCode;
329 if (!TextUtils.isEmpty(subtypeHashCodeStr)) {
330 hashCode = Integer.valueOf(subtypeHashCodeStr);
331 } else {
332 hashCode = 0;
333 }
334 if (hashCode == 0 && !allowImplicitlySelectedSubtype) {
satok3cb5b392011-08-26 11:55:21 +0900335 return null;
satokada8c4e2011-08-23 14:56:56 +0900336 }
satok05f24702011-11-02 19:29:35 +0900337 String candidateLocale = null;
338 if (hashCode == 0) {
339 // Spell checker language settings == "auto"
340 final InputMethodManager imm =
341 (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
342 if (imm != null) {
343 final InputMethodSubtype currentInputMethodSubtype =
344 imm.getCurrentInputMethodSubtype();
345 if (currentInputMethodSubtype != null) {
346 final String localeString = currentInputMethodSubtype.getLocale();
347 if (!TextUtils.isEmpty(localeString)) {
348 // 1. Use keyboard locale if available in the spell checker
349 candidateLocale = localeString;
350 }
351 }
352 }
353 if (candidateLocale == null) {
354 // 2. Use System locale if available in the spell checker
355 candidateLocale = mContext.getResources().getConfiguration().locale.toString();
356 }
357 }
satokb3879542011-08-26 17:35:27 +0900358 SpellCheckerSubtype candidate = null;
satokada8c4e2011-08-23 14:56:56 +0900359 for (int i = 0; i < sci.getSubtypeCount(); ++i) {
360 final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
satokb3879542011-08-26 17:35:27 +0900361 if (hashCode == 0) {
Satoshi Kataoka17150cf2012-05-30 20:05:44 +0900362 final String scsLocale = scs.getLocale();
363 if (candidateLocale.equals(scsLocale)) {
satokb3879542011-08-26 17:35:27 +0900364 return scs;
365 } else if (candidate == null) {
satok7018a902012-05-24 18:10:37 +0900366 if (candidateLocale.length() >= 2 && scsLocale.length() >= 2
367 && candidateLocale.startsWith(scsLocale)) {
satok05f24702011-11-02 19:29:35 +0900368 // Fall back to the applicable language
satokb3879542011-08-26 17:35:27 +0900369 candidate = scs;
370 }
371 }
372 } else if (scs.hashCode() == hashCode) {
satoka33c4fc2011-08-25 16:50:11 +0900373 if (DBG) {
satok70deff42011-09-30 15:14:21 +0900374 Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
375 + ", " + scs.getLocale());
satoka33c4fc2011-08-25 16:50:11 +0900376 }
satok05f24702011-11-02 19:29:35 +0900377 // 3. Use the user specified spell check language
satokada8c4e2011-08-23 14:56:56 +0900378 return scs;
379 }
380 }
satok05f24702011-11-02 19:29:35 +0900381 // 4. Fall back to the applicable language and return it if not null
382 // 5. Simply just return it even if it's null which means we could find no suitable
383 // spell check languages
satokb3879542011-08-26 17:35:27 +0900384 return candidate;
satokada8c4e2011-08-23 14:56:56 +0900385 }
386 }
387
satok988323c2011-06-22 16:38:13 +0900388 @Override
satok5b9b5a92011-08-02 12:24:44 +0900389 public void getSpellCheckerService(String sciId, String locale,
satok53578062011-08-03 16:08:59 +0900390 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
391 Bundle bundle) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900392 if (!calledFromValidUser()) {
393 return;
394 }
satok988323c2011-06-22 16:38:13 +0900395 if (!mSystemReady) {
396 return;
397 }
satok5b9b5a92011-08-02 12:24:44 +0900398 if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) {
satok988323c2011-06-22 16:38:13 +0900399 Slog.e(TAG, "getSpellCheckerService: Invalid input.");
400 return;
401 }
satok988323c2011-06-22 16:38:13 +0900402 synchronized(mSpellCheckerMap) {
403 if (!mSpellCheckerMap.containsKey(sciId)) {
404 return;
405 }
satok5b9b5a92011-08-02 12:24:44 +0900406 final SpellCheckerInfo sci = mSpellCheckerMap.get(sciId);
satokdf5659d2011-07-29 18:38:21 +0900407 final int uid = Binder.getCallingUid();
satok988323c2011-06-22 16:38:13 +0900408 if (mSpellCheckerBindGroups.containsKey(sciId)) {
satok6be6d752011-07-28 20:40:38 +0900409 final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId);
410 if (bindGroup != null) {
411 final InternalDeathRecipient recipient =
412 mSpellCheckerBindGroups.get(sciId).addListener(
satok53578062011-08-03 16:08:59 +0900413 tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900414 if (recipient == null) {
415 if (DBG) {
416 Slog.w(TAG, "Didn't create a death recipient.");
417 }
418 return;
419 }
420 if (bindGroup.mSpellChecker == null & bindGroup.mConnected) {
421 Slog.e(TAG, "The state of the spell checker bind group is illegal.");
422 bindGroup.removeAll();
423 } else if (bindGroup.mSpellChecker != null) {
424 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900425 Slog.w(TAG, "Existing bind found. Return a spell checker session now. "
426 + "Listeners count = " + bindGroup.mListeners.size());
satok6be6d752011-07-28 20:40:38 +0900427 }
428 try {
429 final ISpellCheckerSession session =
430 bindGroup.mSpellChecker.getISpellCheckerSession(
satok53578062011-08-03 16:08:59 +0900431 recipient.mScLocale, recipient.mScListener, bundle);
satokdf5659d2011-07-29 18:38:21 +0900432 if (session != null) {
433 tsListener.onServiceConnected(session);
434 return;
435 } else {
436 if (DBG) {
437 Slog.w(TAG, "Existing bind already expired. ");
438 }
439 bindGroup.removeAll();
440 }
satok6be6d752011-07-28 20:40:38 +0900441 } catch (RemoteException e) {
442 Slog.e(TAG, "Exception in getting spell checker session: " + e);
443 bindGroup.removeAll();
444 }
445 }
446 }
satok988323c2011-06-22 16:38:13 +0900447 }
satok6be6d752011-07-28 20:40:38 +0900448 final long ident = Binder.clearCallingIdentity();
449 try {
satok53578062011-08-03 16:08:59 +0900450 startSpellCheckerServiceInnerLocked(
451 sci, locale, tsListener, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900452 } finally {
453 Binder.restoreCallingIdentity(ident);
satok988323c2011-06-22 16:38:13 +0900454 }
satok988323c2011-06-22 16:38:13 +0900455 }
456 return;
457 }
458
satoka33c4fc2011-08-25 16:50:11 +0900459 @Override
460 public boolean isSpellCheckerEnabled() {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900461 if (!calledFromValidUser()) {
462 return false;
463 }
satoka33c4fc2011-08-25 16:50:11 +0900464 synchronized(mSpellCheckerMap) {
465 return isSpellCheckerEnabledLocked();
466 }
467 }
468
satok6be6d752011-07-28 20:40:38 +0900469 private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale,
satokdf5659d2011-07-29 18:38:21 +0900470 ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
satok53578062011-08-03 16:08:59 +0900471 int uid, Bundle bundle) {
satokdf5659d2011-07-29 18:38:21 +0900472 if (DBG) {
473 Slog.w(TAG, "Start spell checker session inner locked.");
474 }
satok6be6d752011-07-28 20:40:38 +0900475 final String sciId = info.getId();
476 final InternalServiceConnection connection = new InternalServiceConnection(
satok060677f2011-11-17 09:40:56 +0900477 sciId, locale, bundle);
satok6be6d752011-07-28 20:40:38 +0900478 final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE);
479 serviceIntent.setComponent(info.getComponent());
480 if (DBG) {
481 Slog.w(TAG, "bind service: " + info.getId());
482 }
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900483 if (!bindCurrentSpellCheckerService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) {
satok6be6d752011-07-28 20:40:38 +0900484 Slog.e(TAG, "Failed to get a spell checker service.");
485 return;
486 }
487 final SpellCheckerBindGroup group = new SpellCheckerBindGroup(
satok53578062011-08-03 16:08:59 +0900488 connection, tsListener, locale, scListener, uid, bundle);
satok6be6d752011-07-28 20:40:38 +0900489 mSpellCheckerBindGroups.put(sciId, group);
490 }
491
satok988323c2011-06-22 16:38:13 +0900492 @Override
satok562ab582011-07-25 10:12:21 +0900493 public SpellCheckerInfo[] getEnabledSpellCheckers() {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900494 // TODO: Make this work even for non-current users?
495 if (!calledFromValidUser()) {
496 return null;
497 }
satokda317ef2011-07-26 08:02:45 +0900498 if (DBG) {
499 Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size());
500 for (int i = 0; i < mSpellCheckerList.size(); ++i) {
501 Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName());
502 }
503 }
satok562ab582011-07-25 10:12:21 +0900504 return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]);
505 }
506
507 @Override
satok988323c2011-06-22 16:38:13 +0900508 public void finishSpellCheckerService(ISpellCheckerSessionListener listener) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900509 if (!calledFromValidUser()) {
510 return;
511 }
satokda317ef2011-07-26 08:02:45 +0900512 if (DBG) {
513 Slog.d(TAG, "FinishSpellCheckerService");
514 }
satok988323c2011-06-22 16:38:13 +0900515 synchronized(mSpellCheckerMap) {
satok4c3fa642011-11-30 18:17:59 +0900516 final ArrayList<SpellCheckerBindGroup> removeList =
517 new ArrayList<SpellCheckerBindGroup>();
satok988323c2011-06-22 16:38:13 +0900518 for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) {
519 if (group == null) continue;
satok4c3fa642011-11-30 18:17:59 +0900520 // Use removeList to avoid modifying mSpellCheckerBindGroups in this loop.
521 removeList.add(group);
522 }
523 final int removeSize = removeList.size();
524 for (int i = 0; i < removeSize; ++i) {
525 removeList.get(i).removeListener(listener);
satok988323c2011-06-22 16:38:13 +0900526 }
527 }
528 }
529
satokdf5659d2011-07-29 18:38:21 +0900530 @Override
satokada8c4e2011-08-23 14:56:56 +0900531 public void setCurrentSpellChecker(String locale, String sciId) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900532 if (!calledFromValidUser()) {
533 return;
534 }
satokdf5659d2011-07-29 18:38:21 +0900535 synchronized(mSpellCheckerMap) {
536 if (mContext.checkCallingOrSelfPermission(
537 android.Manifest.permission.WRITE_SECURE_SETTINGS)
538 != PackageManager.PERMISSION_GRANTED) {
539 throw new SecurityException(
540 "Requires permission "
541 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
542 }
satok5b9b5a92011-08-02 12:24:44 +0900543 setCurrentSpellCheckerLocked(sciId);
satokdf5659d2011-07-29 18:38:21 +0900544 }
545 }
546
satokada8c4e2011-08-23 14:56:56 +0900547 @Override
548 public void setCurrentSpellCheckerSubtype(String locale, int hashCode) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900549 if (!calledFromValidUser()) {
550 return;
551 }
satokada8c4e2011-08-23 14:56:56 +0900552 synchronized(mSpellCheckerMap) {
553 if (mContext.checkCallingOrSelfPermission(
554 android.Manifest.permission.WRITE_SECURE_SETTINGS)
555 != PackageManager.PERMISSION_GRANTED) {
556 throw new SecurityException(
557 "Requires permission "
558 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
559 }
satoka33c4fc2011-08-25 16:50:11 +0900560 setCurrentSpellCheckerSubtypeLocked(hashCode);
561 }
562 }
563
564 @Override
565 public void setSpellCheckerEnabled(boolean enabled) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900566 if (!calledFromValidUser()) {
567 return;
568 }
satoka33c4fc2011-08-25 16:50:11 +0900569 synchronized(mSpellCheckerMap) {
570 if (mContext.checkCallingOrSelfPermission(
571 android.Manifest.permission.WRITE_SECURE_SETTINGS)
572 != PackageManager.PERMISSION_GRANTED) {
573 throw new SecurityException(
574 "Requires permission "
575 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
576 }
577 setSpellCheckerEnabledLocked(enabled);
satokada8c4e2011-08-23 14:56:56 +0900578 }
579 }
580
satok5b9b5a92011-08-02 12:24:44 +0900581 private void setCurrentSpellCheckerLocked(String sciId) {
satok562ab582011-07-25 10:12:21 +0900582 if (DBG) {
satok5b9b5a92011-08-02 12:24:44 +0900583 Slog.w(TAG, "setCurrentSpellChecker: " + sciId);
satok562ab582011-07-25 10:12:21 +0900584 }
satok5b9b5a92011-08-02 12:24:44 +0900585 if (TextUtils.isEmpty(sciId) || !mSpellCheckerMap.containsKey(sciId)) return;
satokf39daef2011-08-26 19:54:27 +0900586 final SpellCheckerInfo currentSci = getCurrentSpellChecker(null);
587 if (currentSci != null && currentSci.getId().equals(sciId)) {
588 // Do nothing if the current spell checker is same as new spell checker.
589 return;
590 }
satokdf5659d2011-07-29 18:38:21 +0900591 final long ident = Binder.clearCallingIdentity();
592 try {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900593 mSettings.putSelectedSpellChecker(sciId);
satokf39daef2011-08-26 19:54:27 +0900594 setCurrentSpellCheckerSubtypeLocked(0);
satokada8c4e2011-08-23 14:56:56 +0900595 } finally {
596 Binder.restoreCallingIdentity(ident);
597 }
598 }
599
satoka33c4fc2011-08-25 16:50:11 +0900600 private void setCurrentSpellCheckerSubtypeLocked(int hashCode) {
satokada8c4e2011-08-23 14:56:56 +0900601 if (DBG) {
602 Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode);
603 }
604 final SpellCheckerInfo sci = getCurrentSpellChecker(null);
satokfbedf1a2011-08-26 15:48:50 +0900605 int tempHashCode = 0;
606 for (int i = 0; sci != null && i < sci.getSubtypeCount(); ++i) {
satokada8c4e2011-08-23 14:56:56 +0900607 if(sci.getSubtypeAt(i).hashCode() == hashCode) {
satokfbedf1a2011-08-26 15:48:50 +0900608 tempHashCode = hashCode;
satokada8c4e2011-08-23 14:56:56 +0900609 break;
610 }
611 }
satokada8c4e2011-08-23 14:56:56 +0900612 final long ident = Binder.clearCallingIdentity();
613 try {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900614 mSettings.putSelectedSpellCheckerSubtype(tempHashCode);
satokdf5659d2011-07-29 18:38:21 +0900615 } finally {
616 Binder.restoreCallingIdentity(ident);
617 }
satok988323c2011-06-22 16:38:13 +0900618 }
619
satoka33c4fc2011-08-25 16:50:11 +0900620 private void setSpellCheckerEnabledLocked(boolean enabled) {
621 if (DBG) {
622 Slog.w(TAG, "setSpellCheckerEnabled: " + enabled);
623 }
624 final long ident = Binder.clearCallingIdentity();
625 try {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900626 mSettings.setSpellCheckerEnabled(enabled);
satoka33c4fc2011-08-25 16:50:11 +0900627 } finally {
628 Binder.restoreCallingIdentity(ident);
629 }
630 }
631
632 private boolean isSpellCheckerEnabledLocked() {
633 final long ident = Binder.clearCallingIdentity();
634 try {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900635 final boolean retval = mSettings.isSpellCheckerEnabled();
satoka33c4fc2011-08-25 16:50:11 +0900636 if (DBG) {
637 Slog.w(TAG, "getSpellCheckerEnabled: " + retval);
638 }
639 return retval;
640 } finally {
641 Binder.restoreCallingIdentity(ident);
642 }
643 }
644
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700645 @Override
646 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
647 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
648 != PackageManager.PERMISSION_GRANTED) {
649
650 pw.println("Permission Denial: can't dump TextServicesManagerService from from pid="
651 + Binder.getCallingPid()
652 + ", uid=" + Binder.getCallingUid());
653 return;
654 }
655
656 synchronized(mSpellCheckerMap) {
657 pw.println("Current Text Services Manager state:");
658 pw.println(" Spell Checker Map:");
659 for (Map.Entry<String, SpellCheckerInfo> ent : mSpellCheckerMap.entrySet()) {
660 pw.print(" "); pw.print(ent.getKey()); pw.println(":");
661 SpellCheckerInfo info = ent.getValue();
662 pw.print(" "); pw.print("id="); pw.println(info.getId());
663 pw.print(" "); pw.print("comp=");
664 pw.println(info.getComponent().toShortString());
665 int NS = info.getSubtypeCount();
666 for (int i=0; i<NS; i++) {
667 SpellCheckerSubtype st = info.getSubtypeAt(i);
668 pw.print(" "); pw.print("Subtype #"); pw.print(i); pw.println(":");
669 pw.print(" "); pw.print("locale="); pw.println(st.getLocale());
670 pw.print(" "); pw.print("extraValue=");
671 pw.println(st.getExtraValue());
672 }
673 }
674 pw.println("");
675 pw.println(" Spell Checker Bind Groups:");
676 for (Map.Entry<String, SpellCheckerBindGroup> ent
677 : mSpellCheckerBindGroups.entrySet()) {
678 SpellCheckerBindGroup grp = ent.getValue();
679 pw.print(" "); pw.print(ent.getKey()); pw.print(" ");
680 pw.print(grp); pw.println(":");
681 pw.print(" "); pw.print("mInternalConnection=");
682 pw.println(grp.mInternalConnection);
683 pw.print(" "); pw.print("mSpellChecker=");
684 pw.println(grp.mSpellChecker);
685 pw.print(" "); pw.print("mBound="); pw.print(grp.mBound);
686 pw.print(" mConnected="); pw.println(grp.mConnected);
687 int NL = grp.mListeners.size();
688 for (int i=0; i<NL; i++) {
689 InternalDeathRecipient listener = grp.mListeners.get(i);
690 pw.print(" "); pw.print("Listener #"); pw.print(i); pw.println(":");
691 pw.print(" "); pw.print("mTsListener=");
692 pw.println(listener.mTsListener);
693 pw.print(" "); pw.print("mScListener=");
694 pw.println(listener.mScListener);
695 pw.print(" "); pw.print("mGroup=");
696 pw.println(listener.mGroup);
697 pw.print(" "); pw.print("mScLocale=");
698 pw.print(listener.mScLocale);
699 pw.print(" mUid="); pw.println(listener.mUid);
700 }
701 }
702 }
703 }
704
satok988323c2011-06-22 16:38:13 +0900705 // SpellCheckerBindGroup contains active text service session listeners.
706 // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from
707 // mSpellCheckerBindGroups
708 private class SpellCheckerBindGroup {
satokdf5659d2011-07-29 18:38:21 +0900709 private final String TAG = SpellCheckerBindGroup.class.getSimpleName();
satok6be6d752011-07-28 20:40:38 +0900710 private final InternalServiceConnection mInternalConnection;
satok4e713f12012-02-28 16:51:15 +0900711 private final CopyOnWriteArrayList<InternalDeathRecipient> mListeners =
712 new CopyOnWriteArrayList<InternalDeathRecipient>();
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700713 public boolean mBound;
satok6be6d752011-07-28 20:40:38 +0900714 public ISpellCheckerService mSpellChecker;
715 public boolean mConnected;
satok988323c2011-06-22 16:38:13 +0900716
717 public SpellCheckerBindGroup(InternalServiceConnection connection,
718 ITextServicesSessionListener listener, String locale,
satok53578062011-08-03 16:08:59 +0900719 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900720 mInternalConnection = connection;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700721 mBound = true;
satok6be6d752011-07-28 20:40:38 +0900722 mConnected = false;
satok53578062011-08-03 16:08:59 +0900723 addListener(listener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900724 }
725
726 public void onServiceConnected(ISpellCheckerService spellChecker) {
satokda317ef2011-07-26 08:02:45 +0900727 if (DBG) {
728 Slog.d(TAG, "onServiceConnected");
729 }
satok4e713f12012-02-28 16:51:15 +0900730
731 for (InternalDeathRecipient listener : mListeners) {
732 try {
733 final ISpellCheckerSession session = spellChecker.getISpellCheckerSession(
734 listener.mScLocale, listener.mScListener, listener.mBundle);
735 synchronized(mSpellCheckerMap) {
736 if (mListeners.contains(listener)) {
737 listener.mTsListener.onServiceConnected(session);
738 }
satok988323c2011-06-22 16:38:13 +0900739 }
satok4e713f12012-02-28 16:51:15 +0900740 } catch (RemoteException e) {
741 Slog.e(TAG, "Exception in getting the spell checker session."
742 + "Reconnect to the spellchecker. ", e);
743 removeAll();
744 return;
satok988323c2011-06-22 16:38:13 +0900745 }
satok4e713f12012-02-28 16:51:15 +0900746 }
747 synchronized(mSpellCheckerMap) {
satok6be6d752011-07-28 20:40:38 +0900748 mSpellChecker = spellChecker;
749 mConnected = true;
satok988323c2011-06-22 16:38:13 +0900750 }
751 }
752
satok6be6d752011-07-28 20:40:38 +0900753 public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener,
satok53578062011-08-03 16:08:59 +0900754 String locale, ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satokda317ef2011-07-26 08:02:45 +0900755 if (DBG) {
756 Slog.d(TAG, "addListener: " + locale);
757 }
satok6be6d752011-07-28 20:40:38 +0900758 InternalDeathRecipient recipient = null;
satok988323c2011-06-22 16:38:13 +0900759 synchronized(mSpellCheckerMap) {
760 try {
761 final int size = mListeners.size();
762 for (int i = 0; i < size; ++i) {
763 if (mListeners.get(i).hasSpellCheckerListener(scListener)) {
764 // do not add the lister if the group already contains this.
satok6be6d752011-07-28 20:40:38 +0900765 return null;
satok988323c2011-06-22 16:38:13 +0900766 }
767 }
satok6be6d752011-07-28 20:40:38 +0900768 recipient = new InternalDeathRecipient(
satok53578062011-08-03 16:08:59 +0900769 this, tsListener, locale, scListener, uid, bundle);
satok988323c2011-06-22 16:38:13 +0900770 scListener.asBinder().linkToDeath(recipient, 0);
satokdf5659d2011-07-29 18:38:21 +0900771 mListeners.add(recipient);
satok988323c2011-06-22 16:38:13 +0900772 } catch(RemoteException e) {
773 // do nothing
774 }
775 cleanLocked();
776 }
satok6be6d752011-07-28 20:40:38 +0900777 return recipient;
satok988323c2011-06-22 16:38:13 +0900778 }
779
780 public void removeListener(ISpellCheckerSessionListener listener) {
satokda317ef2011-07-26 08:02:45 +0900781 if (DBG) {
satokdf5659d2011-07-29 18:38:21 +0900782 Slog.w(TAG, "remove listener: " + listener.hashCode());
satokda317ef2011-07-26 08:02:45 +0900783 }
satok988323c2011-06-22 16:38:13 +0900784 synchronized(mSpellCheckerMap) {
785 final int size = mListeners.size();
786 final ArrayList<InternalDeathRecipient> removeList =
787 new ArrayList<InternalDeathRecipient>();
788 for (int i = 0; i < size; ++i) {
789 final InternalDeathRecipient tempRecipient = mListeners.get(i);
790 if(tempRecipient.hasSpellCheckerListener(listener)) {
satokdf5659d2011-07-29 18:38:21 +0900791 if (DBG) {
792 Slog.w(TAG, "found existing listener.");
793 }
satok988323c2011-06-22 16:38:13 +0900794 removeList.add(tempRecipient);
795 }
796 }
797 final int removeSize = removeList.size();
798 for (int i = 0; i < removeSize; ++i) {
satokdf5659d2011-07-29 18:38:21 +0900799 if (DBG) {
800 Slog.w(TAG, "Remove " + removeList.get(i));
801 }
satok2520ed82011-10-31 19:38:05 +0900802 final InternalDeathRecipient idr = removeList.get(i);
803 idr.mScListener.asBinder().unlinkToDeath(idr, 0);
804 mListeners.remove(idr);
satok988323c2011-06-22 16:38:13 +0900805 }
806 cleanLocked();
807 }
808 }
809
satok4c3fa642011-11-30 18:17:59 +0900810 // cleanLocked may remove elements from mSpellCheckerBindGroups
satok988323c2011-06-22 16:38:13 +0900811 private void cleanLocked() {
satokda317ef2011-07-26 08:02:45 +0900812 if (DBG) {
813 Slog.d(TAG, "cleanLocked");
814 }
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700815 // If there are no more active listeners, clean up. Only do this
816 // once.
817 if (mBound && mListeners.isEmpty()) {
818 mBound = false;
satokc7b60f722011-08-31 16:30:27 +0900819 final String sciId = mInternalConnection.mSciId;
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700820 SpellCheckerBindGroup cur = mSpellCheckerBindGroups.get(sciId);
821 if (cur == this) {
satokc7b60f722011-08-31 16:30:27 +0900822 if (DBG) {
823 Slog.d(TAG, "Remove bind group.");
824 }
825 mSpellCheckerBindGroups.remove(sciId);
satok6be6d752011-07-28 20:40:38 +0900826 }
satok988323c2011-06-22 16:38:13 +0900827 mContext.unbindService(mInternalConnection);
828 }
829 }
satok6be6d752011-07-28 20:40:38 +0900830
831 public void removeAll() {
832 Slog.e(TAG, "Remove the spell checker bind unexpectedly.");
satokdf5659d2011-07-29 18:38:21 +0900833 synchronized(mSpellCheckerMap) {
satok2520ed82011-10-31 19:38:05 +0900834 final int size = mListeners.size();
835 for (int i = 0; i < size; ++i) {
836 final InternalDeathRecipient idr = mListeners.get(i);
837 idr.mScListener.asBinder().unlinkToDeath(idr, 0);
838 }
satokdf5659d2011-07-29 18:38:21 +0900839 mListeners.clear();
840 cleanLocked();
841 }
satok6be6d752011-07-28 20:40:38 +0900842 }
satok988323c2011-06-22 16:38:13 +0900843 }
844
845 private class InternalServiceConnection implements ServiceConnection {
satok988323c2011-06-22 16:38:13 +0900846 private final String mSciId;
847 private final String mLocale;
satok53578062011-08-03 16:08:59 +0900848 private final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900849 public InternalServiceConnection(
satok060677f2011-11-17 09:40:56 +0900850 String id, String locale, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900851 mSciId = id;
852 mLocale = locale;
satok53578062011-08-03 16:08:59 +0900853 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900854 }
855
856 @Override
857 public void onServiceConnected(ComponentName name, IBinder service) {
858 synchronized(mSpellCheckerMap) {
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900859 onServiceConnectedInnerLocked(name, service);
860 }
861 }
862
863 private void onServiceConnectedInnerLocked(ComponentName name, IBinder service) {
864 if (DBG) {
865 Slog.w(TAG, "onServiceConnected: " + name);
866 }
867 final ISpellCheckerService spellChecker =
868 ISpellCheckerService.Stub.asInterface(service);
869 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
870 if (group != null && this == group.mInternalConnection) {
871 group.onServiceConnected(spellChecker);
satok988323c2011-06-22 16:38:13 +0900872 }
873 }
874
875 @Override
876 public void onServiceDisconnected(ComponentName name) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700877 synchronized(mSpellCheckerMap) {
878 final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId);
satok2cf1cf02011-10-21 15:25:23 +0900879 if (group != null && this == group.mInternalConnection) {
Dianne Hackborn71e14da2011-10-16 16:28:10 -0700880 mSpellCheckerBindGroups.remove(mSciId);
881 }
882 }
satok988323c2011-06-22 16:38:13 +0900883 }
884 }
885
886 private class InternalDeathRecipient implements IBinder.DeathRecipient {
887 public final ITextServicesSessionListener mTsListener;
888 public final ISpellCheckerSessionListener mScListener;
889 public final String mScLocale;
890 private final SpellCheckerBindGroup mGroup;
satokdf5659d2011-07-29 18:38:21 +0900891 public final int mUid;
satok53578062011-08-03 16:08:59 +0900892 public final Bundle mBundle;
satok988323c2011-06-22 16:38:13 +0900893 public InternalDeathRecipient(SpellCheckerBindGroup group,
894 ITextServicesSessionListener tsListener, String scLocale,
satok53578062011-08-03 16:08:59 +0900895 ISpellCheckerSessionListener scListener, int uid, Bundle bundle) {
satok988323c2011-06-22 16:38:13 +0900896 mTsListener = tsListener;
897 mScListener = scListener;
898 mScLocale = scLocale;
899 mGroup = group;
satokdf5659d2011-07-29 18:38:21 +0900900 mUid = uid;
satok53578062011-08-03 16:08:59 +0900901 mBundle = bundle;
satok988323c2011-06-22 16:38:13 +0900902 }
903
904 public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) {
satokdf5659d2011-07-29 18:38:21 +0900905 return listener.asBinder().equals(mScListener.asBinder());
satok988323c2011-06-22 16:38:13 +0900906 }
907
908 @Override
909 public void binderDied() {
910 mGroup.removeListener(mScListener);
911 }
912 }
Satoshi Kataoka00d2d412012-09-28 20:32:33 +0900913
914 private static class TextServicesSettings {
915 private final ContentResolver mResolver;
916 private int mCurrentUserId;
917 public TextServicesSettings(ContentResolver resolver, int userId) {
918 mResolver = resolver;
919 mCurrentUserId = userId;
920 }
921
922 public void setCurrentUserId(int userId) {
923 if (DBG) {
924 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
925 + userId + ", new ime = " + getSelectedSpellChecker());
926 }
927 // TSMS settings are kept per user, so keep track of current user
928 mCurrentUserId = userId;
929 }
930
931 public int getCurrentUserId() {
932 return mCurrentUserId;
933 }
934
935 public void putSelectedSpellChecker(String sciId) {
936 Settings.Secure.putStringForUser(mResolver,
937 Settings.Secure.SELECTED_SPELL_CHECKER, sciId, mCurrentUserId);
938 }
939
940 public void putSelectedSpellCheckerSubtype(int hashCode) {
941 Settings.Secure.putStringForUser(mResolver,
942 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(hashCode),
943 mCurrentUserId);
944 }
945
946 public void setSpellCheckerEnabled(boolean enabled) {
947 Settings.Secure.putIntForUser(mResolver,
948 Settings.Secure.SPELL_CHECKER_ENABLED, enabled ? 1 : 0, mCurrentUserId);
949 }
950
951 public String getSelectedSpellChecker() {
952 return Settings.Secure.getStringForUser(mResolver,
953 Settings.Secure.SELECTED_SPELL_CHECKER, mCurrentUserId);
954 }
955
956 public String getSelectedSpellCheckerSubtype() {
957 return Settings.Secure.getStringForUser(mResolver,
958 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, mCurrentUserId);
959 }
960
961 public boolean isSpellCheckerEnabled() {
962 return Settings.Secure.getIntForUser(mResolver,
963 Settings.Secure.SPELL_CHECKER_ENABLED, 1, mCurrentUserId) == 1;
964 }
965 }
966
967 // ----------------------------------------------------------------------
968 // Utilities for debug
969 private static String getStackTrace() {
970 final StringBuilder sb = new StringBuilder();
971 try {
972 throw new RuntimeException();
973 } catch (RuntimeException e) {
974 final StackTraceElement[] frames = e.getStackTrace();
975 // Start at 1 because the first frame is here and we don't care about it
976 for (int j = 1; j < frames.length; ++j) {
977 sb.append(frames[j].toString() + "\n");
978 }
979 }
980 return sb.toString();
981 }
satok988323c2011-06-22 16:38:13 +0900982}