satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 19 | import static android.view.textservice.TextServicesManager.DISABLE_PER_PROFILE_SPELL_CHECKER; |
| 20 | |
| 21 | import com.android.internal.annotations.GuardedBy; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 22 | import com.android.internal.content.PackageMonitor; |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 23 | import com.android.internal.inputmethod.InputMethodUtils; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 24 | import com.android.internal.textservice.ISpellCheckerService; |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 25 | import com.android.internal.textservice.ISpellCheckerServiceCallback; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 26 | import com.android.internal.textservice.ISpellCheckerSession; |
| 27 | import com.android.internal.textservice.ISpellCheckerSessionListener; |
| 28 | import com.android.internal.textservice.ITextServicesManager; |
| 29 | import com.android.internal.textservice.ITextServicesSessionListener; |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 30 | import com.android.internal.textservice.LazyIntToIntMap; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 31 | import com.android.internal.util.DumpUtils; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 32 | |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 33 | import org.xmlpull.v1.XmlPullParserException; |
| 34 | |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 35 | import android.annotation.NonNull; |
Yohei Yukawa | 08ce187 | 2016-03-16 17:22:30 -0700 | [diff] [blame] | 36 | import android.annotation.Nullable; |
Yohei Yukawa | 9faa2ae | 2016-03-07 13:42:07 -0800 | [diff] [blame] | 37 | import android.annotation.UserIdInt; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 38 | import android.content.ComponentName; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 39 | import android.content.ContentResolver; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
| 42 | import android.content.ServiceConnection; |
Guliz Tuncay | 0f0a37b | 2017-08-16 12:02:31 -0700 | [diff] [blame] | 43 | import android.content.pm.ApplicationInfo; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 44 | import android.content.pm.PackageManager; |
| 45 | import android.content.pm.ResolveInfo; |
| 46 | import android.content.pm.ServiceInfo; |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 47 | import android.content.pm.UserInfo; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 48 | import android.os.Binder; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 49 | import android.os.Bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 50 | import android.os.IBinder; |
Guliz Tuncay | f982e75 | 2017-06-14 09:33:16 -0700 | [diff] [blame] | 51 | import android.os.RemoteCallbackList; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 52 | import android.os.RemoteException; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 53 | import android.os.UserHandle; |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 54 | import android.os.UserManager; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 55 | import android.provider.Settings; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 56 | import android.service.textservice.SpellCheckerService; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 57 | import android.text.TextUtils; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 58 | import android.util.Slog; |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 59 | import android.util.SparseArray; |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 60 | import android.view.inputmethod.InputMethodManager; |
| 61 | import android.view.inputmethod.InputMethodSubtype; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 62 | import android.view.textservice.SpellCheckerInfo; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 63 | import android.view.textservice.SpellCheckerSubtype; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 64 | |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 65 | import java.io.FileDescriptor; |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 66 | import java.io.IOException; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 67 | import java.io.PrintWriter; |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 68 | import java.util.Arrays; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 69 | import java.util.ArrayList; |
| 70 | import java.util.HashMap; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 71 | import java.util.List; |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 72 | import java.util.Locale; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 73 | import java.util.Map; |
Yohei Yukawa | 73ac947 | 2018-05-01 11:40:29 -0700 | [diff] [blame] | 74 | import java.util.function.Predicate; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 75 | |
| 76 | public class TextServicesManagerService extends ITextServicesManager.Stub { |
| 77 | private static final String TAG = TextServicesManagerService.class.getSimpleName(); |
| 78 | private static final boolean DBG = false; |
| 79 | |
| 80 | private final Context mContext; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 81 | private final TextServicesMonitor mMonitor; |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 82 | private final SparseArray<TextServicesData> mUserData = new SparseArray<>(); |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 83 | @NonNull |
| 84 | private final UserManager mUserManager; |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 85 | private final Object mLock = new Object(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 86 | |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 87 | @NonNull |
| 88 | @GuardedBy("mLock") |
| 89 | private final LazyIntToIntMap mSpellCheckerOwnerUserIdMap; |
| 90 | |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 91 | private static class TextServicesData { |
| 92 | @UserIdInt |
| 93 | private final int mUserId; |
| 94 | private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap; |
| 95 | private final ArrayList<SpellCheckerInfo> mSpellCheckerList; |
| 96 | private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups; |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 97 | private final Context mContext; |
| 98 | private final ContentResolver mResolver; |
| 99 | public int mUpdateCount = 0; |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 100 | |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 101 | public TextServicesData(@UserIdInt int userId, @NonNull Context context) { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 102 | mUserId = userId; |
| 103 | mSpellCheckerMap = new HashMap<>(); |
| 104 | mSpellCheckerList = new ArrayList<>(); |
| 105 | mSpellCheckerBindGroups = new HashMap<>(); |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 106 | mContext = context; |
| 107 | mResolver = context.getContentResolver(); |
| 108 | } |
| 109 | |
| 110 | private void putString(final String key, final String str) { |
| 111 | Settings.Secure.putStringForUser(mResolver, key, str, mUserId); |
| 112 | } |
| 113 | |
| 114 | @Nullable |
| 115 | private String getString(@NonNull final String key, @Nullable final String defaultValue) { |
| 116 | final String result; |
| 117 | result = Settings.Secure.getStringForUser(mResolver, key, mUserId); |
| 118 | return result != null ? result : defaultValue; |
| 119 | } |
| 120 | |
| 121 | private void putInt(final String key, final int value) { |
| 122 | Settings.Secure.putIntForUser(mResolver, key, value, mUserId); |
| 123 | } |
| 124 | |
| 125 | private int getInt(final String key, final int defaultValue) { |
| 126 | return Settings.Secure.getIntForUser(mResolver, key, defaultValue, mUserId); |
| 127 | } |
| 128 | |
| 129 | private boolean getBoolean(final String key, final boolean defaultValue) { |
| 130 | return getInt(key, defaultValue ? 1 : 0) == 1; |
| 131 | } |
| 132 | |
| 133 | private void putSelectedSpellChecker(@Nullable String sciId) { |
Guliz Tuncay | 83a7330 | 2017-08-17 15:25:38 -0700 | [diff] [blame] | 134 | putString(Settings.Secure.SELECTED_SPELL_CHECKER, sciId); |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private void putSelectedSpellCheckerSubtype(int hashCode) { |
| 138 | putInt(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, hashCode); |
| 139 | } |
| 140 | |
| 141 | @NonNull |
| 142 | private String getSelectedSpellChecker() { |
| 143 | return getString(Settings.Secure.SELECTED_SPELL_CHECKER, ""); |
| 144 | } |
| 145 | |
| 146 | public int getSelectedSpellCheckerSubtype(final int defaultValue) { |
| 147 | return getInt(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, defaultValue); |
| 148 | } |
| 149 | |
| 150 | public boolean isSpellCheckerEnabled() { |
| 151 | return getBoolean(Settings.Secure.SPELL_CHECKER_ENABLED, true); |
| 152 | } |
| 153 | |
| 154 | @Nullable |
| 155 | public SpellCheckerInfo getCurrentSpellChecker() { |
| 156 | final String curSpellCheckerId = getSelectedSpellChecker(); |
| 157 | if (TextUtils.isEmpty(curSpellCheckerId)) { |
| 158 | return null; |
| 159 | } |
| 160 | return mSpellCheckerMap.get(curSpellCheckerId); |
| 161 | } |
| 162 | |
Guliz Tuncay | 83a7330 | 2017-08-17 15:25:38 -0700 | [diff] [blame] | 163 | public void setCurrentSpellChecker(@Nullable SpellCheckerInfo sci) { |
| 164 | if (sci != null) { |
| 165 | putSelectedSpellChecker(sci.getId()); |
| 166 | } else { |
| 167 | putSelectedSpellChecker(""); |
| 168 | } |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 169 | putSelectedSpellCheckerSubtype(SpellCheckerSubtype.SUBTYPE_ID_NONE); |
| 170 | } |
| 171 | |
| 172 | private void initializeTextServicesData() { |
| 173 | if (DBG) { |
| 174 | Slog.d(TAG, "initializeTextServicesData for user: " + mUserId); |
| 175 | } |
| 176 | mSpellCheckerList.clear(); |
| 177 | mSpellCheckerMap.clear(); |
| 178 | mUpdateCount++; |
| 179 | final PackageManager pm = mContext.getPackageManager(); |
| 180 | // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the |
| 181 | // default behavior of PackageManager is exactly what we want. It by default picks up |
| 182 | // appropriate services depending on the unlock state for the specified user. |
| 183 | final List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
| 184 | new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA, |
| 185 | mUserId); |
| 186 | final int N = services.size(); |
| 187 | for (int i = 0; i < N; ++i) { |
| 188 | final ResolveInfo ri = services.get(i); |
| 189 | final ServiceInfo si = ri.serviceInfo; |
| 190 | final ComponentName compName = new ComponentName(si.packageName, si.name); |
| 191 | if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) { |
| 192 | Slog.w(TAG, "Skipping text service " + compName |
| 193 | + ": it does not require the permission " |
| 194 | + android.Manifest.permission.BIND_TEXT_SERVICE); |
| 195 | continue; |
| 196 | } |
| 197 | if (DBG) Slog.d(TAG, "Add: " + compName + " for user: " + mUserId); |
| 198 | try { |
| 199 | final SpellCheckerInfo sci = new SpellCheckerInfo(mContext, ri); |
| 200 | if (sci.getSubtypeCount() <= 0) { |
| 201 | Slog.w(TAG, "Skipping text service " + compName |
| 202 | + ": it does not contain subtypes."); |
| 203 | continue; |
| 204 | } |
| 205 | mSpellCheckerList.add(sci); |
| 206 | mSpellCheckerMap.put(sci.getId(), sci); |
| 207 | } catch (XmlPullParserException e) { |
| 208 | Slog.w(TAG, "Unable to load the spell checker " + compName, e); |
| 209 | } catch (IOException e) { |
| 210 | Slog.w(TAG, "Unable to load the spell checker " + compName, e); |
| 211 | } |
| 212 | } |
| 213 | if (DBG) { |
| 214 | Slog.d(TAG, "initializeSpellCheckerMap: " + mSpellCheckerList.size() + "," |
| 215 | + mSpellCheckerMap.size()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | private void dump(PrintWriter pw) { |
| 220 | int spellCheckerIndex = 0; |
| 221 | pw.println(" User #" + mUserId); |
| 222 | pw.println(" Spell Checkers:"); |
| 223 | pw.println(" Spell Checkers: " + "mUpdateCount=" + mUpdateCount); |
| 224 | for (final SpellCheckerInfo info : mSpellCheckerMap.values()) { |
| 225 | pw.println(" Spell Checker #" + spellCheckerIndex); |
| 226 | info.dump(pw, " "); |
| 227 | ++spellCheckerIndex; |
| 228 | } |
| 229 | |
| 230 | pw.println(""); |
| 231 | pw.println(" Spell Checker Bind Groups:"); |
| 232 | HashMap<String, SpellCheckerBindGroup> spellCheckerBindGroups = mSpellCheckerBindGroups; |
| 233 | for (final Map.Entry<String, SpellCheckerBindGroup> ent |
| 234 | : spellCheckerBindGroups.entrySet()) { |
| 235 | final SpellCheckerBindGroup grp = ent.getValue(); |
| 236 | pw.println(" " + ent.getKey() + " " + grp + ":"); |
| 237 | pw.println(" " + "mInternalConnection=" + grp.mInternalConnection); |
| 238 | pw.println(" " + "mSpellChecker=" + grp.mSpellChecker); |
| 239 | pw.println(" " + "mUnbindCalled=" + grp.mUnbindCalled); |
| 240 | pw.println(" " + "mConnected=" + grp.mConnected); |
| 241 | final int numPendingSessionRequests = grp.mPendingSessionRequests.size(); |
| 242 | for (int j = 0; j < numPendingSessionRequests; j++) { |
| 243 | final SessionRequest req = grp.mPendingSessionRequests.get(j); |
| 244 | pw.println(" " + "Pending Request #" + j + ":"); |
| 245 | pw.println(" " + "mTsListener=" + req.mTsListener); |
| 246 | pw.println(" " + "mScListener=" + req.mScListener); |
| 247 | pw.println( |
Yohei Yukawa | e462284 | 2018-02-26 11:31:47 +0900 | [diff] [blame] | 248 | " " + "mScLocale=" + req.mLocale + " mUid=" + req.mUid); |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 249 | } |
| 250 | final int numOnGoingSessionRequests = grp.mOnGoingSessionRequests.size(); |
| 251 | for (int j = 0; j < numOnGoingSessionRequests; j++) { |
| 252 | final SessionRequest req = grp.mOnGoingSessionRequests.get(j); |
| 253 | pw.println(" " + "On going Request #" + j + ":"); |
| 254 | ++j; |
| 255 | pw.println(" " + "mTsListener=" + req.mTsListener); |
| 256 | pw.println(" " + "mScListener=" + req.mScListener); |
| 257 | pw.println( |
Yohei Yukawa | e462284 | 2018-02-26 11:31:47 +0900 | [diff] [blame] | 258 | " " + "mScLocale=" + req.mLocale + " mUid=" + req.mUid); |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 259 | } |
| 260 | final int N = grp.mListeners.getRegisteredCallbackCount(); |
| 261 | for (int j = 0; j < N; j++) { |
| 262 | final ISpellCheckerSessionListener mScListener = |
| 263 | grp.mListeners.getRegisteredCallbackItem(j); |
| 264 | pw.println(" " + "Listener #" + j + ":"); |
| 265 | pw.println(" " + "mScListener=" + mScListener); |
| 266 | pw.println(" " + "mGroup=" + grp); |
| 267 | } |
| 268 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Yohei Yukawa | a6a152e | 2016-03-07 13:41:15 -0800 | [diff] [blame] | 272 | public static final class Lifecycle extends SystemService { |
| 273 | private TextServicesManagerService mService; |
| 274 | |
| 275 | public Lifecycle(Context context) { |
| 276 | super(context); |
| 277 | mService = new TextServicesManagerService(context); |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void onStart() { |
| 282 | publishBinderService(Context.TEXT_SERVICES_MANAGER_SERVICE, mService); |
| 283 | } |
| 284 | |
| 285 | @Override |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 286 | public void onStopUser(@UserIdInt int userHandle) { |
| 287 | if (DBG) { |
| 288 | Slog.d(TAG, "onStopUser userId: " + userHandle); |
| 289 | } |
| 290 | mService.onStopUser(userHandle); |
Yohei Yukawa | 9faa2ae | 2016-03-07 13:42:07 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 294 | public void onUnlockUser(@UserIdInt int userHandle) { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 295 | if(DBG) { |
| 296 | Slog.d(TAG, "onUnlockUser userId: " + userHandle); |
| 297 | } |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 298 | // Called on the system server's main looper thread. |
| 299 | // TODO: Dispatch this to a worker thread as needed. |
| 300 | mService.onUnlockUser(userHandle); |
| 301 | } |
Yohei Yukawa | a6a152e | 2016-03-07 13:41:15 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 304 | void onStopUser(@UserIdInt int userId) { |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 305 | synchronized (mLock) { |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 306 | // Clear user ID mapping table. |
| 307 | mSpellCheckerOwnerUserIdMap.delete(userId); |
| 308 | |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 309 | // Clean per-user data |
| 310 | TextServicesData tsd = mUserData.get(userId); |
| 311 | if (tsd == null) return; |
| 312 | |
| 313 | unbindServiceLocked(tsd); // Remove bind groups first |
| 314 | mUserData.remove(userId); // This needs to be done after bind groups are all removed |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | void onUnlockUser(@UserIdInt int userId) { |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 319 | synchronized (mLock) { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 320 | // Initialize internal state for the given user |
| 321 | initializeInternalStateLocked(userId); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | public TextServicesManagerService(Context context) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 326 | mContext = context; |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 327 | mUserManager = mContext.getSystemService(UserManager.class); |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 328 | mSpellCheckerOwnerUserIdMap = new LazyIntToIntMap(callingUserId -> { |
| 329 | if (DISABLE_PER_PROFILE_SPELL_CHECKER) { |
| 330 | final long token = Binder.clearCallingIdentity(); |
| 331 | try { |
| 332 | final UserInfo parent = mUserManager.getProfileParent(callingUserId); |
| 333 | return (parent != null) ? parent.id : callingUserId; |
| 334 | } finally { |
| 335 | Binder.restoreCallingIdentity(token); |
| 336 | } |
| 337 | } else { |
| 338 | return callingUserId; |
| 339 | } |
| 340 | }); |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 341 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 342 | mMonitor = new TextServicesMonitor(); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 343 | mMonitor.register(context, null, UserHandle.ALL, true); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 344 | } |
| 345 | |
Guliz Tuncay | 5e1d5d6 | 2017-07-26 11:19:08 -0700 | [diff] [blame] | 346 | private void initializeInternalStateLocked(@UserIdInt int userId) { |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 347 | // When DISABLE_PER_PROFILE_SPELL_CHECKER is true, we make sure here that work profile users |
| 348 | // will never have non-null TextServicesData for their user ID. |
| 349 | if (DISABLE_PER_PROFILE_SPELL_CHECKER |
| 350 | && userId != mSpellCheckerOwnerUserIdMap.get(userId)) { |
| 351 | return; |
| 352 | } |
| 353 | |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 354 | TextServicesData tsd = mUserData.get(userId); |
| 355 | if (tsd == null) { |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 356 | tsd = new TextServicesData(userId, mContext); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 357 | mUserData.put(userId, tsd); |
| 358 | } |
| 359 | |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 360 | tsd.initializeTextServicesData(); |
| 361 | SpellCheckerInfo sci = tsd.getCurrentSpellChecker(); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 362 | if (sci == null) { |
Guliz Tuncay | 0f0a37b | 2017-08-16 12:02:31 -0700 | [diff] [blame] | 363 | sci = findAvailSystemSpellCheckerLocked(null, tsd); |
Guliz Tuncay | 83a7330 | 2017-08-17 15:25:38 -0700 | [diff] [blame] | 364 | // Set the current spell checker if there is one or more system spell checkers |
| 365 | // available. In this case, "sci" is the first one in the available spell |
| 366 | // checkers. |
| 367 | setCurrentSpellCheckerLocked(sci, tsd); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 368 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 369 | } |
| 370 | |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 371 | private final class TextServicesMonitor extends PackageMonitor { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 372 | @Override |
| 373 | public void onSomePackagesChanged() { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 374 | int userId = getChangingUserId(); |
| 375 | if(DBG) { |
| 376 | Slog.d(TAG, "onSomePackagesChanged: " + userId); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 377 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 378 | |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 379 | synchronized (mLock) { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 380 | TextServicesData tsd = mUserData.get(userId); |
| 381 | if (tsd == null) return; |
| 382 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 383 | // TODO: Update for each locale |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 384 | SpellCheckerInfo sci = tsd.getCurrentSpellChecker(); |
| 385 | tsd.initializeTextServicesData(); |
Guliz Tuncay | 83a7330 | 2017-08-17 15:25:38 -0700 | [diff] [blame] | 386 | // If spell checker is disabled, just return. The user should explicitly |
Satoshi Kataoka | 02260e2 | 2013-08-02 16:22:04 +0900 | [diff] [blame] | 387 | // enable the spell checker. |
Guliz Tuncay | 83a7330 | 2017-08-17 15:25:38 -0700 | [diff] [blame] | 388 | if (!tsd.isSpellCheckerEnabled()) return; |
| 389 | |
| 390 | if (sci == null) { |
| 391 | sci = findAvailSystemSpellCheckerLocked(null, tsd); |
| 392 | // Set the current spell checker if there is one or more system spell checkers |
| 393 | // available. In this case, "sci" is the first one in the available spell |
| 394 | // checkers. |
| 395 | setCurrentSpellCheckerLocked(sci, tsd); |
| 396 | } else { |
| 397 | final String packageName = sci.getPackageName(); |
| 398 | final int change = isPackageDisappearing(packageName); |
| 399 | if (DBG) Slog.d(TAG, "Changing package name: " + packageName); |
Yohei Yukawa | 4993f4e | 2018-05-01 15:22:27 -0700 | [diff] [blame] | 400 | if (change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE) { |
Guliz Tuncay | 83a7330 | 2017-08-17 15:25:38 -0700 | [diff] [blame] | 401 | SpellCheckerInfo availSci = |
| 402 | findAvailSystemSpellCheckerLocked(packageName, tsd); |
| 403 | // Set the spell checker settings if different than before |
| 404 | if (availSci == null |
| 405 | || (availSci != null && !availSci.getId().equals(sci.getId()))) { |
| 406 | setCurrentSpellCheckerLocked(availSci, tsd); |
| 407 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 408 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 414 | private boolean bindCurrentSpellCheckerService( |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 415 | Intent service, ServiceConnection conn, int flags, @UserIdInt int userId) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 416 | if (service == null || conn == null) { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 417 | Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn + |
| 418 | ", userId =" + userId); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 419 | return false; |
| 420 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 421 | return mContext.bindServiceAsUser(service, conn, flags, UserHandle.of(userId)); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 422 | } |
| 423 | |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 424 | private void unbindServiceLocked(TextServicesData tsd) { |
| 425 | HashMap<String, SpellCheckerBindGroup> spellCheckerBindGroups = tsd.mSpellCheckerBindGroups; |
| 426 | for (SpellCheckerBindGroup scbg : spellCheckerBindGroups.values()) { |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 427 | scbg.removeAllLocked(); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 428 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 429 | spellCheckerBindGroups.clear(); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 430 | } |
| 431 | |
Guliz Tuncay | 0f0a37b | 2017-08-16 12:02:31 -0700 | [diff] [blame] | 432 | private SpellCheckerInfo findAvailSystemSpellCheckerLocked(String prefPackage, |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 433 | TextServicesData tsd) { |
Guliz Tuncay | 0f0a37b | 2017-08-16 12:02:31 -0700 | [diff] [blame] | 434 | // Filter the spell checker list to remove spell checker services that are not pre-installed |
| 435 | ArrayList<SpellCheckerInfo> spellCheckerList = new ArrayList<>(); |
| 436 | for (SpellCheckerInfo sci : tsd.mSpellCheckerList) { |
| 437 | if ((sci.getServiceInfo().applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 438 | spellCheckerList.add(sci); |
| 439 | } |
| 440 | } |
| 441 | |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 442 | final int spellCheckersCount = spellCheckerList.size(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 443 | if (spellCheckersCount == 0) { |
| 444 | Slog.w(TAG, "no available spell checker services found"); |
| 445 | return null; |
| 446 | } |
| 447 | if (prefPackage != null) { |
| 448 | for (int i = 0; i < spellCheckersCount; ++i) { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 449 | final SpellCheckerInfo sci = spellCheckerList.get(i); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 450 | if (prefPackage.equals(sci.getPackageName())) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 451 | if (DBG) { |
Guliz Tuncay | 0f0a37b | 2017-08-16 12:02:31 -0700 | [diff] [blame] | 452 | Slog.d(TAG, "findAvailSystemSpellCheckerLocked: " + sci.getPackageName()); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 453 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 454 | return sci; |
| 455 | } |
| 456 | } |
| 457 | } |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 458 | |
| 459 | // Look up a spell checker based on the system locale. |
| 460 | // TODO: Still there is a room to improve in the following logic: e.g., check if the package |
| 461 | // is pre-installed or not. |
| 462 | final Locale systemLocal = mContext.getResources().getConfiguration().locale; |
| 463 | final ArrayList<Locale> suitableLocales = |
| 464 | InputMethodUtils.getSuitableLocalesForSpellChecker(systemLocal); |
| 465 | if (DBG) { |
Guliz Tuncay | 0f0a37b | 2017-08-16 12:02:31 -0700 | [diff] [blame] | 466 | Slog.w(TAG, "findAvailSystemSpellCheckerLocked suitableLocales=" |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 467 | + Arrays.toString(suitableLocales.toArray(new Locale[suitableLocales.size()]))); |
| 468 | } |
| 469 | final int localeCount = suitableLocales.size(); |
| 470 | for (int localeIndex = 0; localeIndex < localeCount; ++localeIndex) { |
| 471 | final Locale locale = suitableLocales.get(localeIndex); |
| 472 | for (int spellCheckersIndex = 0; spellCheckersIndex < spellCheckersCount; |
| 473 | ++spellCheckersIndex) { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 474 | final SpellCheckerInfo info = spellCheckerList.get(spellCheckersIndex); |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 475 | final int subtypeCount = info.getSubtypeCount(); |
| 476 | for (int subtypeIndex = 0; subtypeIndex < subtypeCount; ++subtypeIndex) { |
| 477 | final SpellCheckerSubtype subtype = info.getSubtypeAt(subtypeIndex); |
| 478 | final Locale subtypeLocale = InputMethodUtils.constructLocaleFromString( |
| 479 | subtype.getLocale()); |
| 480 | if (locale.equals(subtypeLocale)) { |
| 481 | // TODO: We may have more spell checkers that fall into this category. |
| 482 | // Ideally we should pick up the most suitable one instead of simply |
| 483 | // returning the first found one. |
| 484 | return info; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 490 | if (spellCheckersCount > 1) { |
| 491 | Slog.w(TAG, "more than one spell checker service found, picking first"); |
| 492 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 493 | return spellCheckerList.get(0); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // TODO: Save SpellCheckerService by supported languages. Currently only one spell |
| 497 | // checker is saved. |
| 498 | @Override |
| 499 | public SpellCheckerInfo getCurrentSpellChecker(String locale) { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 500 | int userId = UserHandle.getCallingUserId(); |
| 501 | synchronized (mLock) { |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 502 | final TextServicesData tsd = getDataFromCallingUserIdLocked(userId); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 503 | if (tsd == null) return null; |
| 504 | |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 505 | return tsd.getCurrentSpellChecker(); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 506 | } |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 507 | } |
| 508 | |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 509 | // TODO: Respect allowImplicitlySelectedSubtype |
Satoshi Kataoka | 17150cf | 2012-05-30 20:05:44 +0900 | [diff] [blame] | 510 | // TODO: Save SpellCheckerSubtype by supported languages by looking at "locale". |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 511 | @Override |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 512 | public SpellCheckerSubtype getCurrentSpellCheckerSubtype( |
| 513 | String locale, boolean allowImplicitlySelectedSubtype) { |
Yohei Yukawa | e3e31a8 | 2016-09-20 06:43:03 -0700 | [diff] [blame] | 514 | final int subtypeHashCode; |
| 515 | final SpellCheckerInfo sci; |
| 516 | final Locale systemLocale; |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 517 | final int userId = UserHandle.getCallingUserId(); |
| 518 | |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 519 | synchronized (mLock) { |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 520 | final TextServicesData tsd = getDataFromCallingUserIdLocked(userId); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 521 | if (tsd == null) return null; |
| 522 | |
Yohei Yukawa | e3e31a8 | 2016-09-20 06:43:03 -0700 | [diff] [blame] | 523 | subtypeHashCode = |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 524 | tsd.getSelectedSpellCheckerSubtype(SpellCheckerSubtype.SUBTYPE_ID_NONE); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 525 | if (DBG) { |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 526 | Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCode); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 527 | } |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 528 | sci = tsd.getCurrentSpellChecker(); |
Yohei Yukawa | e3e31a8 | 2016-09-20 06:43:03 -0700 | [diff] [blame] | 529 | systemLocale = mContext.getResources().getConfiguration().locale; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 530 | } |
Yohei Yukawa | e3e31a8 | 2016-09-20 06:43:03 -0700 | [diff] [blame] | 531 | if (sci == null || sci.getSubtypeCount() == 0) { |
| 532 | if (DBG) { |
| 533 | Slog.w(TAG, "Subtype not found."); |
| 534 | } |
| 535 | return null; |
| 536 | } |
| 537 | if (subtypeHashCode == SpellCheckerSubtype.SUBTYPE_ID_NONE |
| 538 | && !allowImplicitlySelectedSubtype) { |
| 539 | return null; |
| 540 | } |
| 541 | String candidateLocale = null; |
| 542 | if (subtypeHashCode == 0) { |
| 543 | // Spell checker language settings == "auto" |
| 544 | final InputMethodManager imm = mContext.getSystemService(InputMethodManager.class); |
| 545 | if (imm != null) { |
| 546 | final InputMethodSubtype currentInputMethodSubtype = |
| 547 | imm.getCurrentInputMethodSubtype(); |
| 548 | if (currentInputMethodSubtype != null) { |
| 549 | final String localeString = currentInputMethodSubtype.getLocale(); |
| 550 | if (!TextUtils.isEmpty(localeString)) { |
| 551 | // 1. Use keyboard locale if available in the spell checker |
| 552 | candidateLocale = localeString; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | if (candidateLocale == null) { |
| 557 | // 2. Use System locale if available in the spell checker |
| 558 | candidateLocale = systemLocale.toString(); |
| 559 | } |
| 560 | } |
| 561 | SpellCheckerSubtype candidate = null; |
| 562 | for (int i = 0; i < sci.getSubtypeCount(); ++i) { |
| 563 | final SpellCheckerSubtype scs = sci.getSubtypeAt(i); |
| 564 | if (subtypeHashCode == 0) { |
| 565 | final String scsLocale = scs.getLocale(); |
| 566 | if (candidateLocale.equals(scsLocale)) { |
| 567 | return scs; |
| 568 | } else if (candidate == null) { |
| 569 | if (candidateLocale.length() >= 2 && scsLocale.length() >= 2 |
| 570 | && candidateLocale.startsWith(scsLocale)) { |
| 571 | // Fall back to the applicable language |
| 572 | candidate = scs; |
| 573 | } |
| 574 | } |
| 575 | } else if (scs.hashCode() == subtypeHashCode) { |
| 576 | if (DBG) { |
| 577 | Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale |
| 578 | + ", " + scs.getLocale()); |
| 579 | } |
| 580 | // 3. Use the user specified spell check language |
| 581 | return scs; |
| 582 | } |
| 583 | } |
| 584 | // 4. Fall back to the applicable language and return it if not null |
| 585 | // 5. Simply just return it even if it's null which means we could find no suitable |
| 586 | // spell check languages |
| 587 | return candidate; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 588 | } |
| 589 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 590 | @Override |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 591 | public void getSpellCheckerService(String sciId, String locale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 592 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener, |
| 593 | Bundle bundle) { |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 594 | if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 595 | Slog.e(TAG, "getSpellCheckerService: Invalid input."); |
| 596 | return; |
| 597 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 598 | int callingUserId = UserHandle.getCallingUserId(); |
| 599 | |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 600 | synchronized (mLock) { |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 601 | final TextServicesData tsd = getDataFromCallingUserIdLocked(callingUserId); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 602 | if (tsd == null) return; |
| 603 | |
| 604 | HashMap<String, SpellCheckerInfo> spellCheckerMap = tsd.mSpellCheckerMap; |
| 605 | if (!spellCheckerMap.containsKey(sciId)) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 606 | return; |
| 607 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 608 | final SpellCheckerInfo sci = spellCheckerMap.get(sciId); |
| 609 | HashMap<String, SpellCheckerBindGroup> spellCheckerBindGroups = |
| 610 | tsd.mSpellCheckerBindGroups; |
| 611 | SpellCheckerBindGroup bindGroup = spellCheckerBindGroups.get(sciId); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 612 | final int uid = Binder.getCallingUid(); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 613 | if (bindGroup == null) { |
| 614 | final long ident = Binder.clearCallingIdentity(); |
| 615 | try { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 616 | bindGroup = startSpellCheckerServiceInnerLocked(sci, tsd); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 617 | } finally { |
| 618 | Binder.restoreCallingIdentity(ident); |
| 619 | } |
| 620 | if (bindGroup == null) { |
| 621 | // startSpellCheckerServiceInnerLocked failed. |
| 622 | return; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 623 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 624 | } |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 625 | |
| 626 | // Start getISpellCheckerSession async IPC, or just queue the request until the spell |
| 627 | // checker service is bound. |
| 628 | bindGroup.getISpellCheckerSessionOrQueueLocked( |
Guliz Tuncay | 5e1d5d6 | 2017-07-26 11:19:08 -0700 | [diff] [blame] | 629 | new SessionRequest(uid, locale, tsListener, scListener, bundle)); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 630 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 631 | } |
| 632 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 633 | @Override |
| 634 | public boolean isSpellCheckerEnabled() { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 635 | int userId = UserHandle.getCallingUserId(); |
| 636 | |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 637 | synchronized (mLock) { |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 638 | final TextServicesData tsd = getDataFromCallingUserIdLocked(userId); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 639 | if (tsd == null) return false; |
| 640 | |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 641 | return tsd.isSpellCheckerEnabled(); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 645 | @Nullable |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 646 | private SpellCheckerBindGroup startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, |
| 647 | TextServicesData tsd) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 648 | if (DBG) { |
| 649 | Slog.w(TAG, "Start spell checker session inner locked."); |
| 650 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 651 | final String sciId = info.getId(); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 652 | final InternalServiceConnection connection = new InternalServiceConnection(sciId, |
| 653 | tsd.mSpellCheckerBindGroups); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 654 | final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE); |
| 655 | serviceIntent.setComponent(info.getComponent()); |
| 656 | if (DBG) { |
| 657 | Slog.w(TAG, "bind service: " + info.getId()); |
| 658 | } |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 659 | if (!bindCurrentSpellCheckerService(serviceIntent, connection, |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 660 | Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT_BACKGROUND, tsd.mUserId)) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 661 | Slog.e(TAG, "Failed to get a spell checker service."); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 662 | return null; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 663 | } |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 664 | final SpellCheckerBindGroup group = new SpellCheckerBindGroup(connection); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 665 | |
| 666 | tsd.mSpellCheckerBindGroups.put(sciId, group); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 667 | return group; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 668 | } |
| 669 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 670 | @Override |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 671 | public SpellCheckerInfo[] getEnabledSpellCheckers() { |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 672 | int callingUserId = UserHandle.getCallingUserId(); |
| 673 | |
| 674 | synchronized (mLock) { |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 675 | final TextServicesData tsd = getDataFromCallingUserIdLocked(callingUserId); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 676 | if (tsd == null) return null; |
| 677 | |
| 678 | ArrayList<SpellCheckerInfo> spellCheckerList = tsd.mSpellCheckerList; |
| 679 | if (DBG) { |
| 680 | Slog.d(TAG, "getEnabledSpellCheckers: " + spellCheckerList.size()); |
| 681 | for (int i = 0; i < spellCheckerList.size(); ++i) { |
| 682 | Slog.d(TAG, |
| 683 | "EnabledSpellCheckers: " + spellCheckerList.get(i).getPackageName()); |
| 684 | } |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 685 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 686 | return spellCheckerList.toArray(new SpellCheckerInfo[spellCheckerList.size()]); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 687 | } |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | @Override |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 691 | public void finishSpellCheckerService(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 692 | if (DBG) { |
| 693 | Slog.d(TAG, "FinishSpellCheckerService"); |
| 694 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 695 | int userId = UserHandle.getCallingUserId(); |
| 696 | |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 697 | synchronized (mLock) { |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 698 | final TextServicesData tsd = getDataFromCallingUserIdLocked(userId); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 699 | if (tsd == null) return; |
| 700 | |
Yohei Yukawa | 074637f | 2016-03-06 14:34:55 -0800 | [diff] [blame] | 701 | final ArrayList<SpellCheckerBindGroup> removeList = new ArrayList<>(); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 702 | HashMap<String, SpellCheckerBindGroup> spellCheckerBindGroups = |
| 703 | tsd.mSpellCheckerBindGroups; |
| 704 | for (SpellCheckerBindGroup group : spellCheckerBindGroups.values()) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 705 | if (group == null) continue; |
satok | 4c3fa64 | 2011-11-30 18:17:59 +0900 | [diff] [blame] | 706 | // Use removeList to avoid modifying mSpellCheckerBindGroups in this loop. |
| 707 | removeList.add(group); |
| 708 | } |
| 709 | final int removeSize = removeList.size(); |
| 710 | for (int i = 0; i < removeSize; ++i) { |
| 711 | removeList.get(i).removeListener(listener); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
Guliz Tuncay | 83a7330 | 2017-08-17 15:25:38 -0700 | [diff] [blame] | 716 | private void setCurrentSpellCheckerLocked(@Nullable SpellCheckerInfo sci, TextServicesData tsd) { |
| 717 | final String sciId = (sci != null) ? sci.getId() : ""; |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 718 | if (DBG) { |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 719 | Slog.w(TAG, "setCurrentSpellChecker: " + sciId); |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 720 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 721 | final long ident = Binder.clearCallingIdentity(); |
| 722 | try { |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 723 | tsd.setCurrentSpellChecker(sci); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 724 | } finally { |
| 725 | Binder.restoreCallingIdentity(ident); |
| 726 | } |
| 727 | } |
| 728 | |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 729 | @Override |
| 730 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 731 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 732 | |
Yohei Yukawa | 02f646e | 2018-01-24 17:50:32 -0800 | [diff] [blame] | 733 | if (args.length == 0 || (args.length == 1 && args[0].equals("-a"))) { |
| 734 | // Dump all users' data |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 735 | synchronized (mLock) { |
| 736 | pw.println("Current Text Services Manager state:"); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 737 | pw.println(" Users:"); |
| 738 | final int numOfUsers = mUserData.size(); |
| 739 | for (int i = 0; i < numOfUsers; i++) { |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 740 | TextServicesData tsd = mUserData.valueAt(i); |
| 741 | tsd.dump(pw); |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 744 | } else { // Dump a given user's data |
| 745 | if (args.length != 2 || !args[0].equals("--user")) { |
| 746 | pw.println("Invalid arguments to text services." ); |
| 747 | return; |
| 748 | } else { |
| 749 | int userId = Integer.parseInt(args[1]); |
| 750 | UserInfo userInfo = mUserManager.getUserInfo(userId); |
| 751 | if (userInfo == null) { |
| 752 | pw.println("Non-existent user."); |
| 753 | return; |
| 754 | } |
| 755 | TextServicesData tsd = mUserData.get(userId); |
| 756 | if (tsd == null) { |
| 757 | pw.println("User needs to unlock first." ); |
| 758 | return; |
| 759 | } |
| 760 | synchronized (mLock) { |
| 761 | pw.println("Current Text Services Manager state:"); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 762 | pw.println(" User " + userId + ":"); |
Guliz Tuncay | 8e6fa02 | 2017-08-14 16:50:15 -0700 | [diff] [blame] | 763 | tsd.dump(pw); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
Yohei Yukawa | 3f8c568 | 2018-03-01 13:10:23 -0800 | [diff] [blame] | 769 | /** |
| 770 | * @param callingUserId user ID of the calling process |
| 771 | * @return {@link TextServicesData} for the given user. {@code null} if spell checker is not |
| 772 | * temporarily / permanently available for the specified user |
| 773 | */ |
| 774 | @Nullable |
| 775 | private TextServicesData getDataFromCallingUserIdLocked(@UserIdInt int callingUserId) { |
| 776 | final int spellCheckerOwnerUserId = mSpellCheckerOwnerUserIdMap.get(callingUserId); |
| 777 | final TextServicesData data = mUserData.get(spellCheckerOwnerUserId); |
| 778 | if (DISABLE_PER_PROFILE_SPELL_CHECKER) { |
| 779 | if (spellCheckerOwnerUserId != callingUserId) { |
| 780 | // Calling process is running under child profile. |
| 781 | if (data == null) { |
| 782 | return null; |
| 783 | } |
| 784 | final SpellCheckerInfo info = data.getCurrentSpellChecker(); |
| 785 | if (info == null) { |
| 786 | return null; |
| 787 | } |
| 788 | final ServiceInfo serviceInfo = info.getServiceInfo(); |
| 789 | if ((serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 790 | // To be conservative, non pre-installed spell checker services are not allowed |
| 791 | // to be used for child profiles. |
| 792 | return null; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | return data; |
| 797 | } |
| 798 | |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 799 | private static final class SessionRequest { |
Yohei Yukawa | e462284 | 2018-02-26 11:31:47 +0900 | [diff] [blame] | 800 | public final int mUid; |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 801 | @Nullable |
| 802 | public final String mLocale; |
| 803 | @NonNull |
| 804 | public final ITextServicesSessionListener mTsListener; |
| 805 | @NonNull |
| 806 | public final ISpellCheckerSessionListener mScListener; |
| 807 | @Nullable |
| 808 | public final Bundle mBundle; |
| 809 | |
Yohei Yukawa | e462284 | 2018-02-26 11:31:47 +0900 | [diff] [blame] | 810 | SessionRequest(int uid, @Nullable String locale, |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 811 | @NonNull ITextServicesSessionListener tsListener, |
| 812 | @NonNull ISpellCheckerSessionListener scListener, @Nullable Bundle bundle) { |
Yohei Yukawa | e462284 | 2018-02-26 11:31:47 +0900 | [diff] [blame] | 813 | mUid = uid; |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 814 | mLocale = locale; |
| 815 | mTsListener = tsListener; |
| 816 | mScListener = scListener; |
| 817 | mBundle = bundle; |
| 818 | } |
| 819 | } |
| 820 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 821 | // SpellCheckerBindGroup contains active text service session listeners. |
| 822 | // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from |
| 823 | // mSpellCheckerBindGroups |
Yohei Yukawa | 06b4be7 | 2017-01-29 14:08:17 -0800 | [diff] [blame] | 824 | private final class SpellCheckerBindGroup { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 825 | private final String TAG = SpellCheckerBindGroup.class.getSimpleName(); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 826 | private final InternalServiceConnection mInternalConnection; |
Guliz Tuncay | f982e75 | 2017-06-14 09:33:16 -0700 | [diff] [blame] | 827 | private final InternalDeathRecipients mListeners; |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 828 | private boolean mUnbindCalled; |
| 829 | private ISpellCheckerService mSpellChecker; |
| 830 | private boolean mConnected; |
| 831 | private final ArrayList<SessionRequest> mPendingSessionRequests = new ArrayList<>(); |
| 832 | private final ArrayList<SessionRequest> mOnGoingSessionRequests = new ArrayList<>(); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 833 | @NonNull |
| 834 | HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups; |
| 835 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 836 | |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 837 | public SpellCheckerBindGroup(InternalServiceConnection connection) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 838 | mInternalConnection = connection; |
Guliz Tuncay | f982e75 | 2017-06-14 09:33:16 -0700 | [diff] [blame] | 839 | mListeners = new InternalDeathRecipients(this); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 840 | mSpellCheckerBindGroups = connection.mSpellCheckerBindGroups; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 841 | } |
| 842 | |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 843 | public void onServiceConnectedLocked(ISpellCheckerService spellChecker) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 844 | if (DBG) { |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 845 | Slog.d(TAG, "onServiceConnectedLocked"); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 846 | } |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 847 | |
Yohei Yukawa | 4163a96 | 2017-08-07 10:17:59 -0700 | [diff] [blame] | 848 | if (mUnbindCalled) { |
| 849 | return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 850 | } |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 851 | mSpellChecker = spellChecker; |
| 852 | mConnected = true; |
| 853 | // Dispatch pending getISpellCheckerSession requests. |
Yohei Yukawa | 4163a96 | 2017-08-07 10:17:59 -0700 | [diff] [blame] | 854 | try { |
| 855 | final int size = mPendingSessionRequests.size(); |
| 856 | for (int i = 0; i < size; ++i) { |
| 857 | final SessionRequest request = mPendingSessionRequests.get(i); |
| 858 | mSpellChecker.getISpellCheckerSession( |
| 859 | request.mLocale, request.mScListener, request.mBundle, |
| 860 | new ISpellCheckerServiceCallbackBinder(this, request)); |
| 861 | mOnGoingSessionRequests.add(request); |
| 862 | } |
| 863 | mPendingSessionRequests.clear(); |
| 864 | } catch(RemoteException e) { |
| 865 | // The target spell checker service is not available. Better to reset the state. |
| 866 | removeAllLocked(); |
| 867 | } |
| 868 | cleanLocked(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 869 | } |
| 870 | |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 871 | public void onServiceDisconnectedLocked() { |
Guliz Tuncay | 787aa8c | 2017-06-19 17:15:40 -0700 | [diff] [blame] | 872 | if (DBG) { |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 873 | Slog.d(TAG, "onServiceDisconnectedLocked"); |
Guliz Tuncay | 787aa8c | 2017-06-19 17:15:40 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 876 | mSpellChecker = null; |
| 877 | mConnected = false; |
Guliz Tuncay | 787aa8c | 2017-06-19 17:15:40 -0700 | [diff] [blame] | 878 | } |
| 879 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 880 | public void removeListener(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 881 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 882 | Slog.w(TAG, "remove listener: " + listener.hashCode()); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 883 | } |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 884 | synchronized (mLock) { |
Guliz Tuncay | f982e75 | 2017-06-14 09:33:16 -0700 | [diff] [blame] | 885 | mListeners.unregister(listener); |
Yohei Yukawa | 73ac947 | 2018-05-01 11:40:29 -0700 | [diff] [blame] | 886 | final IBinder scListenerBinder = listener.asBinder(); |
| 887 | final Predicate<SessionRequest> removeCondition = |
| 888 | request -> request.mScListener.asBinder() == scListenerBinder; |
| 889 | mPendingSessionRequests.removeIf(removeCondition); |
| 890 | mOnGoingSessionRequests.removeIf(removeCondition); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 891 | cleanLocked(); |
| 892 | } |
| 893 | } |
| 894 | |
satok | 4c3fa64 | 2011-11-30 18:17:59 +0900 | [diff] [blame] | 895 | // cleanLocked may remove elements from mSpellCheckerBindGroups |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 896 | private void cleanLocked() { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 897 | if (DBG) { |
| 898 | Slog.d(TAG, "cleanLocked"); |
| 899 | } |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 900 | if (mUnbindCalled) { |
| 901 | return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 902 | } |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 903 | // If there are no more active listeners, clean up. Only do this once. |
Guliz Tuncay | f982e75 | 2017-06-14 09:33:16 -0700 | [diff] [blame] | 904 | if (mListeners.getRegisteredCallbackCount() > 0) { |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 905 | return; |
| 906 | } |
| 907 | if (!mPendingSessionRequests.isEmpty()) { |
| 908 | return; |
| 909 | } |
| 910 | if (!mOnGoingSessionRequests.isEmpty()) { |
| 911 | return; |
| 912 | } |
| 913 | final String sciId = mInternalConnection.mSciId; |
| 914 | final SpellCheckerBindGroup cur = mSpellCheckerBindGroups.get(sciId); |
| 915 | if (cur == this) { |
| 916 | if (DBG) { |
| 917 | Slog.d(TAG, "Remove bind group."); |
| 918 | } |
| 919 | mSpellCheckerBindGroups.remove(sciId); |
| 920 | } |
| 921 | mContext.unbindService(mInternalConnection); |
| 922 | mUnbindCalled = true; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 923 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 924 | |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 925 | public void removeAllLocked() { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 926 | Slog.e(TAG, "Remove the spell checker bind unexpectedly."); |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 927 | final int size = mListeners.getRegisteredCallbackCount(); |
| 928 | for (int i = size - 1; i >= 0; --i) { |
| 929 | mListeners.unregister(mListeners.getRegisteredCallbackItem(i)); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 930 | } |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 931 | mPendingSessionRequests.clear(); |
| 932 | mOnGoingSessionRequests.clear(); |
| 933 | cleanLocked(); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | public void getISpellCheckerSessionOrQueueLocked(@NonNull SessionRequest request) { |
| 937 | if (mUnbindCalled) { |
| 938 | return; |
| 939 | } |
Yohei Yukawa | 73ac947 | 2018-05-01 11:40:29 -0700 | [diff] [blame] | 940 | mListeners.register(request.mScListener); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 941 | if (!mConnected) { |
| 942 | mPendingSessionRequests.add(request); |
| 943 | return; |
| 944 | } |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 945 | try { |
| 946 | mSpellChecker.getISpellCheckerSession( |
| 947 | request.mLocale, request.mScListener, request.mBundle, |
| 948 | new ISpellCheckerServiceCallbackBinder(this, request)); |
| 949 | mOnGoingSessionRequests.add(request); |
| 950 | } catch(RemoteException e) { |
| 951 | // The target spell checker service is not available. Better to reset the state. |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 952 | removeAllLocked(); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 953 | } |
| 954 | cleanLocked(); |
| 955 | } |
| 956 | |
| 957 | void onSessionCreated(@Nullable final ISpellCheckerSession newSession, |
| 958 | @NonNull final SessionRequest request) { |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 959 | synchronized (mLock) { |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 960 | if (mUnbindCalled) { |
| 961 | return; |
| 962 | } |
| 963 | if (mOnGoingSessionRequests.remove(request)) { |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 964 | try { |
| 965 | request.mTsListener.onServiceConnected(newSession); |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 966 | } catch (RemoteException e) { |
| 967 | // Technically this can happen if the spell checker client app is already |
| 968 | // dead. We can just forget about this request; the request is already |
| 969 | // removed from mOnGoingSessionRequests and the death recipient listener is |
| 970 | // not yet added to mListeners. There is nothing to release further. |
| 971 | } |
| 972 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 973 | cleanLocked(); |
| 974 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 975 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 976 | } |
| 977 | |
Yohei Yukawa | 06b4be7 | 2017-01-29 14:08:17 -0800 | [diff] [blame] | 978 | private final class InternalServiceConnection implements ServiceConnection { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 979 | private final String mSciId; |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 980 | @NonNull |
| 981 | private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups; |
| 982 | public InternalServiceConnection(String id, |
| 983 | @NonNull HashMap<String, SpellCheckerBindGroup> spellCheckerBindGroups) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 984 | mSciId = id; |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 985 | mSpellCheckerBindGroups = spellCheckerBindGroups; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | @Override |
| 989 | public void onServiceConnected(ComponentName name, IBinder service) { |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 990 | synchronized (mLock) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 991 | onServiceConnectedInnerLocked(name, service); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | private void onServiceConnectedInnerLocked(ComponentName name, IBinder service) { |
| 996 | if (DBG) { |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 997 | Slog.w(TAG, "onServiceConnectedInnerLocked: " + name); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 998 | } |
| 999 | final ISpellCheckerService spellChecker = |
| 1000 | ISpellCheckerService.Stub.asInterface(service); |
Guliz Tuncay | 06a2624 | 2017-06-30 18:32:04 -0700 | [diff] [blame] | 1001 | |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1002 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
| 1003 | if (group != null && this == group.mInternalConnection) { |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 1004 | group.onServiceConnectedLocked(spellChecker); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | @Override |
| 1009 | public void onServiceDisconnected(ComponentName name) { |
Guliz Tuncay | 10ae385 | 2017-07-07 10:35:59 -0700 | [diff] [blame] | 1010 | synchronized (mLock) { |
Guliz Tuncay | 787aa8c | 2017-06-19 17:15:40 -0700 | [diff] [blame] | 1011 | onServiceDisconnectedInnerLocked(name); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | private void onServiceDisconnectedInnerLocked(ComponentName name) { |
| 1016 | if (DBG) { |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 1017 | Slog.w(TAG, "onServiceDisconnectedInnerLocked: " + name); |
Guliz Tuncay | 787aa8c | 2017-06-19 17:15:40 -0700 | [diff] [blame] | 1018 | } |
| 1019 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
| 1020 | if (group != null && this == group.mInternalConnection) { |
Yohei Yukawa | 1854cb5 | 2017-08-07 10:17:46 -0700 | [diff] [blame] | 1021 | group.onServiceDisconnectedLocked(); |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 1022 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
Yohei Yukawa | f14fe14 | 2017-06-22 22:30:45 -0700 | [diff] [blame] | 1026 | private static final class InternalDeathRecipients extends |
Guliz Tuncay | f982e75 | 2017-06-14 09:33:16 -0700 | [diff] [blame] | 1027 | RemoteCallbackList<ISpellCheckerSessionListener> { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1028 | private final SpellCheckerBindGroup mGroup; |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 1029 | |
Guliz Tuncay | f982e75 | 2017-06-14 09:33:16 -0700 | [diff] [blame] | 1030 | public InternalDeathRecipients(SpellCheckerBindGroup group) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1031 | mGroup = group; |
| 1032 | } |
| 1033 | |
Guliz Tuncay | f982e75 | 2017-06-14 09:33:16 -0700 | [diff] [blame] | 1034 | @Override |
| 1035 | public void onCallbackDied(ISpellCheckerSessionListener listener) { |
Yohei Yukawa | f14fe14 | 2017-06-22 22:30:45 -0700 | [diff] [blame] | 1036 | mGroup.removeListener(listener); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1037 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1038 | } |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1039 | |
Yohei Yukawa | 7fa65ee | 2017-02-08 11:54:05 -0800 | [diff] [blame] | 1040 | private static final class ISpellCheckerServiceCallbackBinder |
| 1041 | extends ISpellCheckerServiceCallback.Stub { |
| 1042 | @NonNull |
| 1043 | private final SpellCheckerBindGroup mBindGroup; |
| 1044 | @NonNull |
| 1045 | private final SessionRequest mRequest; |
| 1046 | |
| 1047 | ISpellCheckerServiceCallbackBinder(@NonNull final SpellCheckerBindGroup bindGroup, |
| 1048 | @NonNull final SessionRequest request) { |
| 1049 | mBindGroup = bindGroup; |
| 1050 | mRequest = request; |
| 1051 | } |
| 1052 | |
| 1053 | @Override |
| 1054 | public void onSessionCreated(@Nullable ISpellCheckerSession newSession) { |
| 1055 | mBindGroup.onSessionCreated(newSession, mRequest); |
| 1056 | } |
| 1057 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1058 | } |