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