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 | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 19 | import com.android.internal.annotations.GuardedBy; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 20 | import com.android.internal.content.PackageMonitor; |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 21 | import com.android.internal.inputmethod.InputMethodUtils; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 22 | import com.android.internal.textservice.ISpellCheckerService; |
| 23 | import com.android.internal.textservice.ISpellCheckerSession; |
| 24 | import com.android.internal.textservice.ISpellCheckerSessionListener; |
| 25 | import com.android.internal.textservice.ITextServicesManager; |
| 26 | import com.android.internal.textservice.ITextServicesSessionListener; |
| 27 | |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 28 | import org.xmlpull.v1.XmlPullParserException; |
| 29 | |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 30 | import android.annotation.NonNull; |
Yohei Yukawa | 08ce187 | 2016-03-16 17:22:30 -0700 | [diff] [blame] | 31 | import android.annotation.Nullable; |
Yohei Yukawa | 9faa2ae | 2016-03-07 13:42:07 -0800 | [diff] [blame] | 32 | import android.annotation.UserIdInt; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 33 | import android.app.ActivityManagerNative; |
| 34 | import android.app.AppGlobals; |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 35 | import android.content.BroadcastReceiver; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 36 | import android.content.ComponentName; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 37 | import android.content.ContentResolver; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 40 | import android.content.IntentFilter; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 41 | import android.content.ServiceConnection; |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 42 | import android.content.pm.ApplicationInfo; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
| 44 | import android.content.pm.ResolveInfo; |
| 45 | import android.content.pm.ServiceInfo; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 46 | import android.os.Binder; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 47 | import android.os.Bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 48 | import android.os.IBinder; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 49 | import android.os.Process; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 50 | import android.os.RemoteException; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 51 | import android.os.UserHandle; |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 52 | import android.os.UserManager; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 53 | import android.provider.Settings; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 54 | import android.service.textservice.SpellCheckerService; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 55 | import android.text.TextUtils; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 56 | import android.util.Slog; |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 57 | import android.view.inputmethod.InputMethodManager; |
| 58 | import android.view.inputmethod.InputMethodSubtype; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 59 | import android.view.textservice.SpellCheckerInfo; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 60 | import android.view.textservice.SpellCheckerSubtype; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 61 | |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 62 | import java.io.FileDescriptor; |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 63 | import java.io.IOException; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 64 | import java.io.PrintWriter; |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 65 | import java.util.Arrays; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 66 | import java.util.ArrayList; |
| 67 | import java.util.HashMap; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 68 | import java.util.List; |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 69 | import java.util.Locale; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 70 | import java.util.Map; |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 71 | import java.util.concurrent.CopyOnWriteArrayList; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 72 | |
| 73 | public class TextServicesManagerService extends ITextServicesManager.Stub { |
| 74 | private static final String TAG = TextServicesManagerService.class.getSimpleName(); |
| 75 | private static final boolean DBG = false; |
| 76 | |
| 77 | private final Context mContext; |
| 78 | private boolean mSystemReady; |
| 79 | private final TextServicesMonitor mMonitor; |
Yohei Yukawa | 074637f | 2016-03-06 14:34:55 -0800 | [diff] [blame] | 80 | private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap = new HashMap<>(); |
| 81 | private final ArrayList<SpellCheckerInfo> mSpellCheckerList = new ArrayList<>(); |
| 82 | private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups = new HashMap<>(); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 83 | private final TextServicesSettings mSettings; |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 84 | @NonNull |
| 85 | private final UserManager mUserManager; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 86 | |
Yohei Yukawa | a6a152e | 2016-03-07 13:41:15 -0800 | [diff] [blame] | 87 | public static final class Lifecycle extends SystemService { |
| 88 | private TextServicesManagerService mService; |
| 89 | |
| 90 | public Lifecycle(Context context) { |
| 91 | super(context); |
| 92 | mService = new TextServicesManagerService(context); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onStart() { |
| 97 | publishBinderService(Context.TEXT_SERVICES_MANAGER_SERVICE, mService); |
| 98 | } |
| 99 | |
| 100 | @Override |
Yohei Yukawa | 9faa2ae | 2016-03-07 13:42:07 -0800 | [diff] [blame] | 101 | public void onSwitchUser(@UserIdInt int userHandle) { |
| 102 | // Called on the system server's main looper thread. |
| 103 | // TODO: Dispatch this to a worker thread as needed. |
| 104 | mService.onSwitchUser(userHandle); |
| 105 | } |
| 106 | |
| 107 | @Override |
Yohei Yukawa | a6a152e | 2016-03-07 13:41:15 -0800 | [diff] [blame] | 108 | public void onBootPhase(int phase) { |
| 109 | // Called on the system server's main looper thread. |
| 110 | // TODO: Dispatch this to a worker thread as needed. |
| 111 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 112 | mService.systemRunning(); |
| 113 | } |
| 114 | } |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 115 | |
| 116 | @Override |
| 117 | public void onUnlockUser(@UserIdInt int userHandle) { |
| 118 | // Called on the system server's main looper thread. |
| 119 | // TODO: Dispatch this to a worker thread as needed. |
| 120 | mService.onUnlockUser(userHandle); |
| 121 | } |
Yohei Yukawa | a6a152e | 2016-03-07 13:41:15 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void systemRunning() { |
| 125 | synchronized (mSpellCheckerMap) { |
| 126 | if (!mSystemReady) { |
| 127 | mSystemReady = true; |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 128 | resetInternalState(mSettings.getCurrentUserId()); |
Yohei Yukawa | a6a152e | 2016-03-07 13:41:15 -0800 | [diff] [blame] | 129 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Yohei Yukawa | 9faa2ae | 2016-03-07 13:42:07 -0800 | [diff] [blame] | 133 | void onSwitchUser(@UserIdInt int userId) { |
| 134 | synchronized (mSpellCheckerMap) { |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 135 | resetInternalState(userId); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void onUnlockUser(@UserIdInt int userId) { |
| 140 | synchronized(mSpellCheckerMap) { |
| 141 | final int currentUserId = mSettings.getCurrentUserId(); |
| 142 | if (userId != currentUserId) { |
| 143 | return; |
| 144 | } |
| 145 | resetInternalState(currentUserId); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | public TextServicesManagerService(Context context) { |
| 150 | mSystemReady = false; |
| 151 | mContext = context; |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 152 | |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 153 | mUserManager = mContext.getSystemService(UserManager.class); |
| 154 | |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 155 | final IntentFilter broadcastFilter = new IntentFilter(); |
| 156 | broadcastFilter.addAction(Intent.ACTION_USER_ADDED); |
| 157 | broadcastFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 158 | mContext.registerReceiver(new TextServicesBroadcastReceiver(), broadcastFilter); |
| 159 | |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 160 | int userId = UserHandle.USER_SYSTEM; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 161 | try { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 162 | userId = ActivityManagerNative.getDefault().getCurrentUser().id; |
| 163 | } catch (RemoteException e) { |
| 164 | Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e); |
| 165 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 166 | mMonitor = new TextServicesMonitor(); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 167 | mMonitor.register(context, null, true); |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 168 | final boolean useCopyOnWriteSettings = |
| 169 | !mSystemReady || !mUserManager.isUserUnlocked(userId); |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 170 | mSettings = new TextServicesSettings(context.getContentResolver(), userId, |
| 171 | useCopyOnWriteSettings); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 172 | |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 173 | // "resetInternalState" initializes the states for the foreground user |
| 174 | resetInternalState(userId); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 177 | private void resetInternalState(@UserIdInt int userId) { |
| 178 | final boolean useCopyOnWriteSettings = |
| 179 | !mSystemReady || !mUserManager.isUserUnlocked(userId); |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 180 | mSettings.switchCurrentUser(userId, useCopyOnWriteSettings); |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 181 | updateCurrentProfileIds(); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 182 | unbindServiceLocked(); |
| 183 | buildSpellCheckerMapLocked(mContext, mSpellCheckerList, mSpellCheckerMap, mSettings); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 184 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
| 185 | if (sci == null) { |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 186 | sci = findAvailSpellCheckerLocked(null); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 187 | if (sci != null) { |
| 188 | // Set the current spell checker if there is one or more spell checkers |
| 189 | // available. In this case, "sci" is the first one in the available spell |
| 190 | // checkers. |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 191 | setCurrentSpellCheckerLocked(sci.getId()); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 192 | } |
| 193 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 194 | } |
| 195 | |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 196 | void updateCurrentProfileIds() { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 197 | mSettings.setCurrentProfileIds( |
| 198 | mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId())); |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 199 | } |
| 200 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 201 | private class TextServicesMonitor extends PackageMonitor { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 202 | private boolean isChangingPackagesOfCurrentUser() { |
| 203 | final int userId = getChangingUserId(); |
| 204 | final boolean retval = userId == mSettings.getCurrentUserId(); |
| 205 | if (DBG) { |
| 206 | Slog.d(TAG, "--- ignore this call back from a background user: " + userId); |
| 207 | } |
| 208 | return retval; |
| 209 | } |
| 210 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 211 | @Override |
| 212 | public void onSomePackagesChanged() { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 213 | if (!isChangingPackagesOfCurrentUser()) { |
| 214 | return; |
| 215 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 216 | synchronized (mSpellCheckerMap) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 217 | buildSpellCheckerMapLocked( |
| 218 | mContext, mSpellCheckerList, mSpellCheckerMap, mSettings); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 219 | // TODO: Update for each locale |
| 220 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
Satoshi Kataoka | 02260e2 | 2013-08-02 16:22:04 +0900 | [diff] [blame] | 221 | // If no spell checker is enabled, just return. The user should explicitly |
| 222 | // enable the spell checker. |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 223 | if (sci == null) return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 224 | final String packageName = sci.getPackageName(); |
| 225 | final int change = isPackageDisappearing(packageName); |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 226 | if (// Package disappearing |
| 227 | change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE |
| 228 | // Package modified |
| 229 | || isPackageModified(packageName)) { |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 230 | sci = findAvailSpellCheckerLocked(packageName); |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 231 | if (sci != null) { |
| 232 | setCurrentSpellCheckerLocked(sci.getId()); |
| 233 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 239 | class TextServicesBroadcastReceiver extends BroadcastReceiver { |
| 240 | @Override |
| 241 | public void onReceive(Context context, Intent intent) { |
| 242 | final String action = intent.getAction(); |
| 243 | if (Intent.ACTION_USER_ADDED.equals(action) |
| 244 | || Intent.ACTION_USER_REMOVED.equals(action)) { |
| 245 | updateCurrentProfileIds(); |
| 246 | return; |
| 247 | } |
| 248 | Slog.w(TAG, "Unexpected intent " + intent); |
| 249 | } |
| 250 | } |
| 251 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 252 | private static void buildSpellCheckerMapLocked(Context context, |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 253 | ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map, |
| 254 | TextServicesSettings settings) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 255 | list.clear(); |
| 256 | map.clear(); |
| 257 | final PackageManager pm = context.getPackageManager(); |
Yohei Yukawa | f0f1680 | 2016-03-08 16:04:58 -0800 | [diff] [blame] | 258 | // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default |
| 259 | // behavior of PackageManager is exactly what we want. It by default picks up appropriate |
| 260 | // services depending on the unlock state for the specified user. |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 261 | final List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
| 262 | new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA, |
| 263 | settings.getCurrentUserId()); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 264 | final int N = services.size(); |
| 265 | for (int i = 0; i < N; ++i) { |
| 266 | final ResolveInfo ri = services.get(i); |
| 267 | final ServiceInfo si = ri.serviceInfo; |
| 268 | final ComponentName compName = new ComponentName(si.packageName, si.name); |
| 269 | if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) { |
| 270 | Slog.w(TAG, "Skipping text service " + compName |
| 271 | + ": it does not require the permission " |
| 272 | + android.Manifest.permission.BIND_TEXT_SERVICE); |
| 273 | continue; |
| 274 | } |
| 275 | if (DBG) Slog.d(TAG, "Add: " + compName); |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 276 | try { |
| 277 | final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri); |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 278 | if (sci.getSubtypeCount() <= 0) { |
| 279 | Slog.w(TAG, "Skipping text service " + compName |
| 280 | + ": it does not contain subtypes."); |
| 281 | continue; |
| 282 | } |
satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame] | 283 | list.add(sci); |
| 284 | map.put(sci.getId(), sci); |
| 285 | } catch (XmlPullParserException e) { |
| 286 | Slog.w(TAG, "Unable to load the spell checker " + compName, e); |
| 287 | } catch (IOException e) { |
| 288 | Slog.w(TAG, "Unable to load the spell checker " + compName, e); |
| 289 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 290 | } |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 291 | if (DBG) { |
| 292 | Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size()); |
| 293 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 294 | } |
| 295 | |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 296 | // --------------------------------------------------------------------------------------- |
| 297 | // Check whether or not this is a valid IPC. Assumes an IPC is valid when either |
| 298 | // 1) it comes from the system process |
| 299 | // 2) the calling process' user id is identical to the current user id TSMS thinks. |
| 300 | private boolean calledFromValidUser() { |
| 301 | final int uid = Binder.getCallingUid(); |
| 302 | final int userId = UserHandle.getUserId(uid); |
| 303 | if (DBG) { |
| 304 | Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? " |
| 305 | + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID |
| 306 | + " calling userId = " + userId + ", foreground user id = " |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 307 | + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 308 | try { |
| 309 | final String[] packageNames = AppGlobals.getPackageManager().getPackagesForUid(uid); |
| 310 | for (int i = 0; i < packageNames.length; ++i) { |
| 311 | if (DBG) { |
| 312 | Slog.d(TAG, "--- process name for "+ uid + " = " + packageNames[i]); |
| 313 | } |
| 314 | } |
| 315 | } catch (RemoteException e) { |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) { |
| 320 | return true; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 321 | } |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 322 | |
| 323 | // Permits current profile to use TSFM as long as the current text service is the system's |
| 324 | // one. This is a tentative solution and should be replaced with fully functional multiuser |
| 325 | // support. |
| 326 | // TODO: Implement multiuser support in TSMS. |
| 327 | final boolean isCurrentProfile = mSettings.isCurrentProfile(userId); |
| 328 | if (DBG) { |
| 329 | Slog.d(TAG, "--- userId = "+ userId + " isCurrentProfile = " + isCurrentProfile); |
| 330 | } |
| 331 | if (mSettings.isCurrentProfile(userId)) { |
| 332 | final SpellCheckerInfo spellCheckerInfo = getCurrentSpellCheckerWithoutVerification(); |
| 333 | if (spellCheckerInfo != null) { |
| 334 | final ServiceInfo serviceInfo = spellCheckerInfo.getServiceInfo(); |
| 335 | final boolean isSystemSpellChecker = |
| 336 | (serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 337 | if (DBG) { |
| 338 | Slog.d(TAG, "--- current spell checker = "+ spellCheckerInfo.getPackageName() |
| 339 | + " isSystem = " + isSystemSpellChecker); |
| 340 | } |
| 341 | if (isSystemSpellChecker) { |
| 342 | return true; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Unlike InputMethodManagerService#calledFromValidUser, INTERACT_ACROSS_USERS_FULL isn't |
| 348 | // taken into account here. Anyway this method is supposed to be removed once multiuser |
| 349 | // support is implemented. |
| 350 | if (DBG) { |
| 351 | Slog.d(TAG, "--- IPC from userId:" + userId + " is being ignored. \n" |
| 352 | + getStackTrace()); |
| 353 | } |
| 354 | return false; |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | private boolean bindCurrentSpellCheckerService( |
| 358 | Intent service, ServiceConnection conn, int flags) { |
| 359 | if (service == null || conn == null) { |
| 360 | Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn); |
| 361 | return false; |
| 362 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 363 | return mContext.bindServiceAsUser(service, conn, flags, |
| 364 | new UserHandle(mSettings.getCurrentUserId())); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | private void unbindServiceLocked() { |
| 368 | for (SpellCheckerBindGroup scbg : mSpellCheckerBindGroups.values()) { |
| 369 | scbg.removeAll(); |
| 370 | } |
| 371 | mSpellCheckerBindGroups.clear(); |
| 372 | } |
| 373 | |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 374 | private SpellCheckerInfo findAvailSpellCheckerLocked(String prefPackage) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 375 | final int spellCheckersCount = mSpellCheckerList.size(); |
| 376 | if (spellCheckersCount == 0) { |
| 377 | Slog.w(TAG, "no available spell checker services found"); |
| 378 | return null; |
| 379 | } |
| 380 | if (prefPackage != null) { |
| 381 | for (int i = 0; i < spellCheckersCount; ++i) { |
| 382 | final SpellCheckerInfo sci = mSpellCheckerList.get(i); |
| 383 | if (prefPackage.equals(sci.getPackageName())) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 384 | if (DBG) { |
| 385 | Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName()); |
| 386 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 387 | return sci; |
| 388 | } |
| 389 | } |
| 390 | } |
Yohei Yukawa | 174843a | 2015-06-26 18:02:54 -0700 | [diff] [blame] | 391 | |
| 392 | // Look up a spell checker based on the system locale. |
| 393 | // TODO: Still there is a room to improve in the following logic: e.g., check if the package |
| 394 | // is pre-installed or not. |
| 395 | final Locale systemLocal = mContext.getResources().getConfiguration().locale; |
| 396 | final ArrayList<Locale> suitableLocales = |
| 397 | InputMethodUtils.getSuitableLocalesForSpellChecker(systemLocal); |
| 398 | if (DBG) { |
| 399 | Slog.w(TAG, "findAvailSpellCheckerLocked suitableLocales=" |
| 400 | + Arrays.toString(suitableLocales.toArray(new Locale[suitableLocales.size()]))); |
| 401 | } |
| 402 | final int localeCount = suitableLocales.size(); |
| 403 | for (int localeIndex = 0; localeIndex < localeCount; ++localeIndex) { |
| 404 | final Locale locale = suitableLocales.get(localeIndex); |
| 405 | for (int spellCheckersIndex = 0; spellCheckersIndex < spellCheckersCount; |
| 406 | ++spellCheckersIndex) { |
| 407 | final SpellCheckerInfo info = mSpellCheckerList.get(spellCheckersIndex); |
| 408 | final int subtypeCount = info.getSubtypeCount(); |
| 409 | for (int subtypeIndex = 0; subtypeIndex < subtypeCount; ++subtypeIndex) { |
| 410 | final SpellCheckerSubtype subtype = info.getSubtypeAt(subtypeIndex); |
| 411 | final Locale subtypeLocale = InputMethodUtils.constructLocaleFromString( |
| 412 | subtype.getLocale()); |
| 413 | if (locale.equals(subtypeLocale)) { |
| 414 | // TODO: We may have more spell checkers that fall into this category. |
| 415 | // Ideally we should pick up the most suitable one instead of simply |
| 416 | // returning the first found one. |
| 417 | return info; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 423 | if (spellCheckersCount > 1) { |
| 424 | Slog.w(TAG, "more than one spell checker service found, picking first"); |
| 425 | } |
| 426 | return mSpellCheckerList.get(0); |
| 427 | } |
| 428 | |
| 429 | // TODO: Save SpellCheckerService by supported languages. Currently only one spell |
| 430 | // checker is saved. |
| 431 | @Override |
| 432 | public SpellCheckerInfo getCurrentSpellChecker(String locale) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 433 | // TODO: Make this work even for non-current users? |
| 434 | if (!calledFromValidUser()) { |
| 435 | return null; |
| 436 | } |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 437 | return getCurrentSpellCheckerWithoutVerification(); |
| 438 | } |
| 439 | |
| 440 | private SpellCheckerInfo getCurrentSpellCheckerWithoutVerification() { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 441 | synchronized (mSpellCheckerMap) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 442 | final String curSpellCheckerId = mSettings.getSelectedSpellChecker(); |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 443 | if (DBG) { |
| 444 | Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId); |
| 445 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 446 | if (TextUtils.isEmpty(curSpellCheckerId)) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 447 | return null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 448 | } |
| 449 | return mSpellCheckerMap.get(curSpellCheckerId); |
| 450 | } |
| 451 | } |
| 452 | |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 453 | // TODO: Respect allowImplicitlySelectedSubtype |
Satoshi Kataoka | 17150cf | 2012-05-30 20:05:44 +0900 | [diff] [blame] | 454 | // TODO: Save SpellCheckerSubtype by supported languages by looking at "locale". |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 455 | @Override |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 456 | public SpellCheckerSubtype getCurrentSpellCheckerSubtype( |
| 457 | String locale, boolean allowImplicitlySelectedSubtype) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 458 | // TODO: Make this work even for non-current users? |
| 459 | if (!calledFromValidUser()) { |
| 460 | return null; |
| 461 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 462 | synchronized (mSpellCheckerMap) { |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 463 | final int subtypeHashCode = |
| 464 | mSettings.getSelectedSpellCheckerSubtype(SpellCheckerSubtype.SUBTYPE_ID_NONE); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 465 | if (DBG) { |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 466 | Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCode); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 467 | } |
| 468 | final SpellCheckerInfo sci = getCurrentSpellChecker(null); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 469 | if (sci == null || sci.getSubtypeCount() == 0) { |
| 470 | if (DBG) { |
| 471 | Slog.w(TAG, "Subtype not found."); |
| 472 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 473 | return null; |
| 474 | } |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 475 | if (subtypeHashCode == SpellCheckerSubtype.SUBTYPE_ID_NONE |
| 476 | && !allowImplicitlySelectedSubtype) { |
satok | 3cb5b39 | 2011-08-26 11:55:21 +0900 | [diff] [blame] | 477 | return null; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 478 | } |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 479 | String candidateLocale = null; |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 480 | if (subtypeHashCode == 0) { |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 481 | // Spell checker language settings == "auto" |
Yohei Yukawa | 777ef95 | 2015-11-25 20:32:24 -0800 | [diff] [blame] | 482 | final InputMethodManager imm = mContext.getSystemService(InputMethodManager.class); |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 483 | if (imm != null) { |
| 484 | final InputMethodSubtype currentInputMethodSubtype = |
| 485 | imm.getCurrentInputMethodSubtype(); |
| 486 | if (currentInputMethodSubtype != null) { |
| 487 | final String localeString = currentInputMethodSubtype.getLocale(); |
| 488 | if (!TextUtils.isEmpty(localeString)) { |
| 489 | // 1. Use keyboard locale if available in the spell checker |
| 490 | candidateLocale = localeString; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | if (candidateLocale == null) { |
| 495 | // 2. Use System locale if available in the spell checker |
| 496 | candidateLocale = mContext.getResources().getConfiguration().locale.toString(); |
| 497 | } |
| 498 | } |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 499 | SpellCheckerSubtype candidate = null; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 500 | for (int i = 0; i < sci.getSubtypeCount(); ++i) { |
| 501 | final SpellCheckerSubtype scs = sci.getSubtypeAt(i); |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 502 | if (subtypeHashCode == 0) { |
Satoshi Kataoka | 17150cf | 2012-05-30 20:05:44 +0900 | [diff] [blame] | 503 | final String scsLocale = scs.getLocale(); |
| 504 | if (candidateLocale.equals(scsLocale)) { |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 505 | return scs; |
| 506 | } else if (candidate == null) { |
satok | 7018a90 | 2012-05-24 18:10:37 +0900 | [diff] [blame] | 507 | if (candidateLocale.length() >= 2 && scsLocale.length() >= 2 |
| 508 | && candidateLocale.startsWith(scsLocale)) { |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 509 | // Fall back to the applicable language |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 510 | candidate = scs; |
| 511 | } |
| 512 | } |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 513 | } else if (scs.hashCode() == subtypeHashCode) { |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 514 | if (DBG) { |
satok | 70deff4 | 2011-09-30 15:14:21 +0900 | [diff] [blame] | 515 | Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale |
| 516 | + ", " + scs.getLocale()); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 517 | } |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 518 | // 3. Use the user specified spell check language |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 519 | return scs; |
| 520 | } |
| 521 | } |
satok | 05f2470 | 2011-11-02 19:29:35 +0900 | [diff] [blame] | 522 | // 4. Fall back to the applicable language and return it if not null |
| 523 | // 5. Simply just return it even if it's null which means we could find no suitable |
| 524 | // spell check languages |
satok | b387954 | 2011-08-26 17:35:27 +0900 | [diff] [blame] | 525 | return candidate; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 529 | @Override |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 530 | public void getSpellCheckerService(String sciId, String locale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 531 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener, |
| 532 | Bundle bundle) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 533 | if (!calledFromValidUser()) { |
| 534 | return; |
| 535 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 536 | if (!mSystemReady) { |
| 537 | return; |
| 538 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 539 | if (TextUtils.isEmpty(sciId) || tsListener == null || scListener == null) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 540 | Slog.e(TAG, "getSpellCheckerService: Invalid input."); |
| 541 | return; |
| 542 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 543 | synchronized(mSpellCheckerMap) { |
| 544 | if (!mSpellCheckerMap.containsKey(sciId)) { |
| 545 | return; |
| 546 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 547 | final SpellCheckerInfo sci = mSpellCheckerMap.get(sciId); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 548 | final int uid = Binder.getCallingUid(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 549 | if (mSpellCheckerBindGroups.containsKey(sciId)) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 550 | final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId); |
| 551 | if (bindGroup != null) { |
| 552 | final InternalDeathRecipient recipient = |
| 553 | mSpellCheckerBindGroups.get(sciId).addListener( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 554 | tsListener, locale, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 555 | if (recipient == null) { |
| 556 | if (DBG) { |
| 557 | Slog.w(TAG, "Didn't create a death recipient."); |
| 558 | } |
| 559 | return; |
| 560 | } |
| 561 | if (bindGroup.mSpellChecker == null & bindGroup.mConnected) { |
| 562 | Slog.e(TAG, "The state of the spell checker bind group is illegal."); |
| 563 | bindGroup.removeAll(); |
| 564 | } else if (bindGroup.mSpellChecker != null) { |
| 565 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 566 | Slog.w(TAG, "Existing bind found. Return a spell checker session now. " |
| 567 | + "Listeners count = " + bindGroup.mListeners.size()); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 568 | } |
| 569 | try { |
| 570 | final ISpellCheckerSession session = |
| 571 | bindGroup.mSpellChecker.getISpellCheckerSession( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 572 | recipient.mScLocale, recipient.mScListener, bundle); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 573 | if (session != null) { |
| 574 | tsListener.onServiceConnected(session); |
| 575 | return; |
| 576 | } else { |
| 577 | if (DBG) { |
| 578 | Slog.w(TAG, "Existing bind already expired. "); |
| 579 | } |
| 580 | bindGroup.removeAll(); |
| 581 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 582 | } catch (RemoteException e) { |
| 583 | Slog.e(TAG, "Exception in getting spell checker session: " + e); |
| 584 | bindGroup.removeAll(); |
| 585 | } |
| 586 | } |
| 587 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 588 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 589 | final long ident = Binder.clearCallingIdentity(); |
| 590 | try { |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 591 | startSpellCheckerServiceInnerLocked( |
| 592 | sci, locale, tsListener, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 593 | } finally { |
| 594 | Binder.restoreCallingIdentity(ident); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 595 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 596 | } |
| 597 | return; |
| 598 | } |
| 599 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 600 | @Override |
| 601 | public boolean isSpellCheckerEnabled() { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 602 | if (!calledFromValidUser()) { |
| 603 | return false; |
| 604 | } |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 605 | synchronized(mSpellCheckerMap) { |
| 606 | return isSpellCheckerEnabledLocked(); |
| 607 | } |
| 608 | } |
| 609 | |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 610 | private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale, |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 611 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 612 | int uid, Bundle bundle) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 613 | if (DBG) { |
| 614 | Slog.w(TAG, "Start spell checker session inner locked."); |
| 615 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 616 | final String sciId = info.getId(); |
| 617 | final InternalServiceConnection connection = new InternalServiceConnection( |
satok | 060677f | 2011-11-17 09:40:56 +0900 | [diff] [blame] | 618 | sciId, locale, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 619 | final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE); |
| 620 | serviceIntent.setComponent(info.getComponent()); |
| 621 | if (DBG) { |
| 622 | Slog.w(TAG, "bind service: " + info.getId()); |
| 623 | } |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 624 | if (!bindCurrentSpellCheckerService(serviceIntent, connection, |
| 625 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE)) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 626 | Slog.e(TAG, "Failed to get a spell checker service."); |
| 627 | return; |
| 628 | } |
| 629 | final SpellCheckerBindGroup group = new SpellCheckerBindGroup( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 630 | connection, tsListener, locale, scListener, uid, bundle); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 631 | mSpellCheckerBindGroups.put(sciId, group); |
| 632 | } |
| 633 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 634 | @Override |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 635 | public SpellCheckerInfo[] getEnabledSpellCheckers() { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 636 | // TODO: Make this work even for non-current users? |
| 637 | if (!calledFromValidUser()) { |
| 638 | return null; |
| 639 | } |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 640 | if (DBG) { |
| 641 | Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size()); |
| 642 | for (int i = 0; i < mSpellCheckerList.size(); ++i) { |
| 643 | Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName()); |
| 644 | } |
| 645 | } |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 646 | return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]); |
| 647 | } |
| 648 | |
| 649 | @Override |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 650 | public void finishSpellCheckerService(ISpellCheckerSessionListener listener) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 651 | if (!calledFromValidUser()) { |
| 652 | return; |
| 653 | } |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 654 | if (DBG) { |
| 655 | Slog.d(TAG, "FinishSpellCheckerService"); |
| 656 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 657 | synchronized(mSpellCheckerMap) { |
Yohei Yukawa | 074637f | 2016-03-06 14:34:55 -0800 | [diff] [blame] | 658 | final ArrayList<SpellCheckerBindGroup> removeList = new ArrayList<>(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 659 | for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) { |
| 660 | if (group == null) continue; |
satok | 4c3fa64 | 2011-11-30 18:17:59 +0900 | [diff] [blame] | 661 | // Use removeList to avoid modifying mSpellCheckerBindGroups in this loop. |
| 662 | removeList.add(group); |
| 663 | } |
| 664 | final int removeSize = removeList.size(); |
| 665 | for (int i = 0; i < removeSize; ++i) { |
| 666 | removeList.get(i).removeListener(listener); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 671 | @Override |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 672 | public void setCurrentSpellChecker(String locale, String sciId) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 673 | if (!calledFromValidUser()) { |
| 674 | return; |
| 675 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 676 | synchronized(mSpellCheckerMap) { |
| 677 | if (mContext.checkCallingOrSelfPermission( |
| 678 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 679 | != PackageManager.PERMISSION_GRANTED) { |
| 680 | throw new SecurityException( |
| 681 | "Requires permission " |
| 682 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 683 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 684 | setCurrentSpellCheckerLocked(sciId); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 688 | @Override |
| 689 | public void setCurrentSpellCheckerSubtype(String locale, int hashCode) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 690 | if (!calledFromValidUser()) { |
| 691 | return; |
| 692 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 693 | synchronized(mSpellCheckerMap) { |
| 694 | if (mContext.checkCallingOrSelfPermission( |
| 695 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 696 | != PackageManager.PERMISSION_GRANTED) { |
| 697 | throw new SecurityException( |
| 698 | "Requires permission " |
| 699 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 700 | } |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 701 | setCurrentSpellCheckerSubtypeLocked(hashCode); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | @Override |
| 706 | public void setSpellCheckerEnabled(boolean enabled) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 707 | if (!calledFromValidUser()) { |
| 708 | return; |
| 709 | } |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 710 | synchronized(mSpellCheckerMap) { |
| 711 | if (mContext.checkCallingOrSelfPermission( |
| 712 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 713 | != PackageManager.PERMISSION_GRANTED) { |
| 714 | throw new SecurityException( |
| 715 | "Requires permission " |
| 716 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 717 | } |
| 718 | setSpellCheckerEnabledLocked(enabled); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 722 | private void setCurrentSpellCheckerLocked(String sciId) { |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 723 | if (DBG) { |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 724 | Slog.w(TAG, "setCurrentSpellChecker: " + sciId); |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 725 | } |
satok | 5b9b5a9 | 2011-08-02 12:24:44 +0900 | [diff] [blame] | 726 | if (TextUtils.isEmpty(sciId) || !mSpellCheckerMap.containsKey(sciId)) return; |
satok | f39daef | 2011-08-26 19:54:27 +0900 | [diff] [blame] | 727 | final SpellCheckerInfo currentSci = getCurrentSpellChecker(null); |
| 728 | if (currentSci != null && currentSci.getId().equals(sciId)) { |
| 729 | // Do nothing if the current spell checker is same as new spell checker. |
| 730 | return; |
| 731 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 732 | final long ident = Binder.clearCallingIdentity(); |
| 733 | try { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 734 | mSettings.putSelectedSpellChecker(sciId); |
satok | f39daef | 2011-08-26 19:54:27 +0900 | [diff] [blame] | 735 | setCurrentSpellCheckerSubtypeLocked(0); |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 736 | } finally { |
| 737 | Binder.restoreCallingIdentity(ident); |
| 738 | } |
| 739 | } |
| 740 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 741 | private void setCurrentSpellCheckerSubtypeLocked(int hashCode) { |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 742 | if (DBG) { |
| 743 | Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode); |
| 744 | } |
| 745 | final SpellCheckerInfo sci = getCurrentSpellChecker(null); |
satok | fbedf1a | 2011-08-26 15:48:50 +0900 | [diff] [blame] | 746 | int tempHashCode = 0; |
| 747 | for (int i = 0; sci != null && i < sci.getSubtypeCount(); ++i) { |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 748 | if(sci.getSubtypeAt(i).hashCode() == hashCode) { |
satok | fbedf1a | 2011-08-26 15:48:50 +0900 | [diff] [blame] | 749 | tempHashCode = hashCode; |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | } |
satok | ada8c4e | 2011-08-23 14:56:56 +0900 | [diff] [blame] | 753 | final long ident = Binder.clearCallingIdentity(); |
| 754 | try { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 755 | mSettings.putSelectedSpellCheckerSubtype(tempHashCode); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 756 | } finally { |
| 757 | Binder.restoreCallingIdentity(ident); |
| 758 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 759 | } |
| 760 | |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 761 | private void setSpellCheckerEnabledLocked(boolean enabled) { |
| 762 | if (DBG) { |
| 763 | Slog.w(TAG, "setSpellCheckerEnabled: " + enabled); |
| 764 | } |
| 765 | final long ident = Binder.clearCallingIdentity(); |
| 766 | try { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 767 | mSettings.setSpellCheckerEnabled(enabled); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 768 | } finally { |
| 769 | Binder.restoreCallingIdentity(ident); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | private boolean isSpellCheckerEnabledLocked() { |
| 774 | final long ident = Binder.clearCallingIdentity(); |
| 775 | try { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 776 | final boolean retval = mSettings.isSpellCheckerEnabled(); |
satok | a33c4fc | 2011-08-25 16:50:11 +0900 | [diff] [blame] | 777 | if (DBG) { |
| 778 | Slog.w(TAG, "getSpellCheckerEnabled: " + retval); |
| 779 | } |
| 780 | return retval; |
| 781 | } finally { |
| 782 | Binder.restoreCallingIdentity(ident); |
| 783 | } |
| 784 | } |
| 785 | |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 786 | @Override |
| 787 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 788 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 789 | != PackageManager.PERMISSION_GRANTED) { |
| 790 | |
| 791 | pw.println("Permission Denial: can't dump TextServicesManagerService from from pid=" |
| 792 | + Binder.getCallingPid() |
| 793 | + ", uid=" + Binder.getCallingUid()); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | synchronized(mSpellCheckerMap) { |
| 798 | pw.println("Current Text Services Manager state:"); |
Yohei Yukawa | 85df698 | 2016-03-08 15:16:07 -0800 | [diff] [blame] | 799 | pw.println(" Spell Checkers:"); |
| 800 | int spellCheckerIndex = 0; |
| 801 | for (final SpellCheckerInfo info : mSpellCheckerMap.values()) { |
| 802 | pw.println(" Spell Checker #" + spellCheckerIndex); |
| 803 | info.dump(pw, " "); |
| 804 | ++spellCheckerIndex; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 805 | } |
| 806 | pw.println(""); |
| 807 | pw.println(" Spell Checker Bind Groups:"); |
Yohei Yukawa | 85df698 | 2016-03-08 15:16:07 -0800 | [diff] [blame] | 808 | for (final Map.Entry<String, SpellCheckerBindGroup> ent |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 809 | : mSpellCheckerBindGroups.entrySet()) { |
Yohei Yukawa | 85df698 | 2016-03-08 15:16:07 -0800 | [diff] [blame] | 810 | final SpellCheckerBindGroup grp = ent.getValue(); |
| 811 | pw.println(" " + ent.getKey() + " " + grp + ":"); |
| 812 | pw.println(" " + "mInternalConnection=" + grp.mInternalConnection); |
| 813 | pw.println(" " + "mSpellChecker=" + grp.mSpellChecker); |
| 814 | pw.println(" " + "mBound=" + grp.mBound + " mConnected=" + grp.mConnected); |
| 815 | final int N = grp.mListeners.size(); |
| 816 | for (int i = 0; i < N; i++) { |
| 817 | final InternalDeathRecipient listener = grp.mListeners.get(i); |
| 818 | pw.println(" " + "Listener #" + i + ":"); |
| 819 | pw.println(" " + "mTsListener=" + listener.mTsListener); |
| 820 | pw.println(" " + "mScListener=" + listener.mScListener); |
| 821 | pw.println(" " + "mGroup=" + listener.mGroup); |
| 822 | pw.println(" " + "mScLocale=" + listener.mScLocale |
| 823 | + " mUid=" + listener.mUid); |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 824 | } |
| 825 | } |
Yohei Yukawa | 85df698 | 2016-03-08 15:16:07 -0800 | [diff] [blame] | 826 | pw.println(""); |
| 827 | pw.println(" mSettings:"); |
| 828 | mSettings.dumpLocked(pw, " "); |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 832 | // SpellCheckerBindGroup contains active text service session listeners. |
| 833 | // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from |
| 834 | // mSpellCheckerBindGroups |
| 835 | private class SpellCheckerBindGroup { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 836 | private final String TAG = SpellCheckerBindGroup.class.getSimpleName(); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 837 | private final InternalServiceConnection mInternalConnection; |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 838 | private final CopyOnWriteArrayList<InternalDeathRecipient> mListeners = |
Yohei Yukawa | 074637f | 2016-03-06 14:34:55 -0800 | [diff] [blame] | 839 | new CopyOnWriteArrayList<>(); |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 840 | public boolean mBound; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 841 | public ISpellCheckerService mSpellChecker; |
| 842 | public boolean mConnected; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 843 | |
| 844 | public SpellCheckerBindGroup(InternalServiceConnection connection, |
| 845 | ITextServicesSessionListener listener, String locale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 846 | ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 847 | mInternalConnection = connection; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 848 | mBound = true; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 849 | mConnected = false; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 850 | addListener(listener, locale, scListener, uid, bundle); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | public void onServiceConnected(ISpellCheckerService spellChecker) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 854 | if (DBG) { |
| 855 | Slog.d(TAG, "onServiceConnected"); |
| 856 | } |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 857 | |
| 858 | for (InternalDeathRecipient listener : mListeners) { |
| 859 | try { |
| 860 | final ISpellCheckerSession session = spellChecker.getISpellCheckerSession( |
| 861 | listener.mScLocale, listener.mScListener, listener.mBundle); |
| 862 | synchronized(mSpellCheckerMap) { |
| 863 | if (mListeners.contains(listener)) { |
| 864 | listener.mTsListener.onServiceConnected(session); |
| 865 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 866 | } |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 867 | } catch (RemoteException e) { |
| 868 | Slog.e(TAG, "Exception in getting the spell checker session." |
| 869 | + "Reconnect to the spellchecker. ", e); |
| 870 | removeAll(); |
| 871 | return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 872 | } |
satok | 4e713f1 | 2012-02-28 16:51:15 +0900 | [diff] [blame] | 873 | } |
| 874 | synchronized(mSpellCheckerMap) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 875 | mSpellChecker = spellChecker; |
| 876 | mConnected = true; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 880 | public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 881 | String locale, ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 882 | if (DBG) { |
| 883 | Slog.d(TAG, "addListener: " + locale); |
| 884 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 885 | InternalDeathRecipient recipient = null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 886 | synchronized(mSpellCheckerMap) { |
| 887 | try { |
| 888 | final int size = mListeners.size(); |
| 889 | for (int i = 0; i < size; ++i) { |
| 890 | if (mListeners.get(i).hasSpellCheckerListener(scListener)) { |
| 891 | // do not add the lister if the group already contains this. |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 892 | return null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 893 | } |
| 894 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 895 | recipient = new InternalDeathRecipient( |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 896 | this, tsListener, locale, scListener, uid, bundle); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 897 | scListener.asBinder().linkToDeath(recipient, 0); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 898 | mListeners.add(recipient); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 899 | } catch(RemoteException e) { |
| 900 | // do nothing |
| 901 | } |
| 902 | cleanLocked(); |
| 903 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 904 | return recipient; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | public void removeListener(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 908 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 909 | Slog.w(TAG, "remove listener: " + listener.hashCode()); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 910 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 911 | synchronized(mSpellCheckerMap) { |
| 912 | final int size = mListeners.size(); |
Yohei Yukawa | 074637f | 2016-03-06 14:34:55 -0800 | [diff] [blame] | 913 | final ArrayList<InternalDeathRecipient> removeList = new ArrayList<>(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 914 | for (int i = 0; i < size; ++i) { |
| 915 | final InternalDeathRecipient tempRecipient = mListeners.get(i); |
| 916 | if(tempRecipient.hasSpellCheckerListener(listener)) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 917 | if (DBG) { |
| 918 | Slog.w(TAG, "found existing listener."); |
| 919 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 920 | removeList.add(tempRecipient); |
| 921 | } |
| 922 | } |
| 923 | final int removeSize = removeList.size(); |
| 924 | for (int i = 0; i < removeSize; ++i) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 925 | if (DBG) { |
| 926 | Slog.w(TAG, "Remove " + removeList.get(i)); |
| 927 | } |
satok | 2520ed8 | 2011-10-31 19:38:05 +0900 | [diff] [blame] | 928 | final InternalDeathRecipient idr = removeList.get(i); |
| 929 | idr.mScListener.asBinder().unlinkToDeath(idr, 0); |
| 930 | mListeners.remove(idr); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 931 | } |
| 932 | cleanLocked(); |
| 933 | } |
| 934 | } |
| 935 | |
satok | 4c3fa64 | 2011-11-30 18:17:59 +0900 | [diff] [blame] | 936 | // cleanLocked may remove elements from mSpellCheckerBindGroups |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 937 | private void cleanLocked() { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 938 | if (DBG) { |
| 939 | Slog.d(TAG, "cleanLocked"); |
| 940 | } |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 941 | // If there are no more active listeners, clean up. Only do this |
| 942 | // once. |
| 943 | if (mBound && mListeners.isEmpty()) { |
| 944 | mBound = false; |
satok | c7b60f72 | 2011-08-31 16:30:27 +0900 | [diff] [blame] | 945 | final String sciId = mInternalConnection.mSciId; |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 946 | SpellCheckerBindGroup cur = mSpellCheckerBindGroups.get(sciId); |
| 947 | if (cur == this) { |
satok | c7b60f72 | 2011-08-31 16:30:27 +0900 | [diff] [blame] | 948 | if (DBG) { |
| 949 | Slog.d(TAG, "Remove bind group."); |
| 950 | } |
| 951 | mSpellCheckerBindGroups.remove(sciId); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 952 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 953 | mContext.unbindService(mInternalConnection); |
| 954 | } |
| 955 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 956 | |
| 957 | public void removeAll() { |
| 958 | Slog.e(TAG, "Remove the spell checker bind unexpectedly."); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 959 | synchronized(mSpellCheckerMap) { |
satok | 2520ed8 | 2011-10-31 19:38:05 +0900 | [diff] [blame] | 960 | final int size = mListeners.size(); |
| 961 | for (int i = 0; i < size; ++i) { |
| 962 | final InternalDeathRecipient idr = mListeners.get(i); |
| 963 | idr.mScListener.asBinder().unlinkToDeath(idr, 0); |
| 964 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 965 | mListeners.clear(); |
| 966 | cleanLocked(); |
| 967 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 968 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | private class InternalServiceConnection implements ServiceConnection { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 972 | private final String mSciId; |
| 973 | private final String mLocale; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 974 | private final Bundle mBundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 975 | public InternalServiceConnection( |
satok | 060677f | 2011-11-17 09:40:56 +0900 | [diff] [blame] | 976 | String id, String locale, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 977 | mSciId = id; |
| 978 | mLocale = locale; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 979 | mBundle = bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | @Override |
| 983 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 984 | synchronized(mSpellCheckerMap) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 985 | onServiceConnectedInnerLocked(name, service); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | private void onServiceConnectedInnerLocked(ComponentName name, IBinder service) { |
| 990 | if (DBG) { |
| 991 | Slog.w(TAG, "onServiceConnected: " + name); |
| 992 | } |
| 993 | final ISpellCheckerService spellChecker = |
| 994 | ISpellCheckerService.Stub.asInterface(service); |
| 995 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
| 996 | if (group != null && this == group.mInternalConnection) { |
| 997 | group.onServiceConnected(spellChecker); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | @Override |
| 1002 | public void onServiceDisconnected(ComponentName name) { |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 1003 | synchronized(mSpellCheckerMap) { |
| 1004 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
satok | 2cf1cf0 | 2011-10-21 15:25:23 +0900 | [diff] [blame] | 1005 | if (group != null && this == group.mInternalConnection) { |
Dianne Hackborn | 71e14da | 2011-10-16 16:28:10 -0700 | [diff] [blame] | 1006 | mSpellCheckerBindGroups.remove(mSciId); |
| 1007 | } |
| 1008 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | private class InternalDeathRecipient implements IBinder.DeathRecipient { |
| 1013 | public final ITextServicesSessionListener mTsListener; |
| 1014 | public final ISpellCheckerSessionListener mScListener; |
| 1015 | public final String mScLocale; |
| 1016 | private final SpellCheckerBindGroup mGroup; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 1017 | public final int mUid; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 1018 | public final Bundle mBundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1019 | public InternalDeathRecipient(SpellCheckerBindGroup group, |
| 1020 | ITextServicesSessionListener tsListener, String scLocale, |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 1021 | ISpellCheckerSessionListener scListener, int uid, Bundle bundle) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1022 | mTsListener = tsListener; |
| 1023 | mScListener = scListener; |
| 1024 | mScLocale = scLocale; |
| 1025 | mGroup = group; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 1026 | mUid = uid; |
satok | 5357806 | 2011-08-03 16:08:59 +0900 | [diff] [blame] | 1027 | mBundle = bundle; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame] | 1031 | return listener.asBinder().equals(mScListener.asBinder()); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | @Override |
| 1035 | public void binderDied() { |
| 1036 | mGroup.removeListener(mScListener); |
| 1037 | } |
| 1038 | } |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1039 | |
| 1040 | private static class TextServicesSettings { |
| 1041 | private final ContentResolver mResolver; |
Yohei Yukawa | 9faa2ae | 2016-03-07 13:42:07 -0800 | [diff] [blame] | 1042 | @UserIdInt |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1043 | private int mCurrentUserId; |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 1044 | @GuardedBy("mLock") |
| 1045 | private int[] mCurrentProfileIds = new int[0]; |
| 1046 | private Object mLock = new Object(); |
| 1047 | |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1048 | /** |
| 1049 | * On-memory data store to emulate when {@link #mCopyOnWrite} is {@code true}. |
| 1050 | */ |
| 1051 | private final HashMap<String, String> mCopyOnWriteDataStore = new HashMap<>(); |
| 1052 | private boolean mCopyOnWrite = false; |
| 1053 | |
| 1054 | public TextServicesSettings(ContentResolver resolver, @UserIdInt int userId, |
| 1055 | boolean copyOnWrite) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1056 | mResolver = resolver; |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1057 | switchCurrentUser(userId, copyOnWrite); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1058 | } |
| 1059 | |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1060 | /** |
| 1061 | * Must be called when the current user is changed. |
| 1062 | * |
| 1063 | * @param userId The user ID. |
| 1064 | * @param copyOnWrite If {@code true}, for each settings key |
| 1065 | * (e.g. {@link Settings.Secure#SELECTED_SPELL_CHECKER}) we use the actual settings on the |
| 1066 | * {@link Settings.Secure} until we do the first write operation. |
| 1067 | */ |
| 1068 | public void switchCurrentUser(@UserIdInt int userId, boolean copyOnWrite) { |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1069 | if (DBG) { |
| 1070 | Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to " |
| 1071 | + userId + ", new ime = " + getSelectedSpellChecker()); |
| 1072 | } |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1073 | if (mCurrentUserId != userId || mCopyOnWrite != copyOnWrite) { |
| 1074 | mCopyOnWriteDataStore.clear(); |
| 1075 | // TODO: mCurrentProfileIds should be cleared here. |
| 1076 | } |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1077 | // TSMS settings are kept per user, so keep track of current user |
| 1078 | mCurrentUserId = userId; |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1079 | mCopyOnWrite = copyOnWrite; |
| 1080 | // TODO: mCurrentProfileIds should be updated here. |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1081 | } |
| 1082 | |
Yohei Yukawa | f0ed48d | 2016-03-07 21:26:38 -0800 | [diff] [blame] | 1083 | private void putString(final String key, final String str) { |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1084 | if (mCopyOnWrite) { |
| 1085 | mCopyOnWriteDataStore.put(key, str); |
| 1086 | } else { |
| 1087 | Settings.Secure.putStringForUser(mResolver, key, str, mCurrentUserId); |
| 1088 | } |
Yohei Yukawa | f0ed48d | 2016-03-07 21:26:38 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
Yohei Yukawa | 08ce187 | 2016-03-16 17:22:30 -0700 | [diff] [blame] | 1091 | @Nullable |
| 1092 | private String getString(@NonNull final String key, @Nullable final String defaultValue) { |
| 1093 | final String result; |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1094 | if (mCopyOnWrite && mCopyOnWriteDataStore.containsKey(key)) { |
Yohei Yukawa | 08ce187 | 2016-03-16 17:22:30 -0700 | [diff] [blame] | 1095 | result = mCopyOnWriteDataStore.get(key); |
| 1096 | } else { |
| 1097 | result = Settings.Secure.getStringForUser(mResolver, key, mCurrentUserId); |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1098 | } |
Yohei Yukawa | 08ce187 | 2016-03-16 17:22:30 -0700 | [diff] [blame] | 1099 | return result != null ? result : defaultValue; |
Yohei Yukawa | f0ed48d | 2016-03-07 21:26:38 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | private void putInt(final String key, final int value) { |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1103 | if (mCopyOnWrite) { |
| 1104 | mCopyOnWriteDataStore.put(key, String.valueOf(value)); |
| 1105 | } else { |
| 1106 | Settings.Secure.putIntForUser(mResolver, key, value, mCurrentUserId); |
| 1107 | } |
Yohei Yukawa | f0ed48d | 2016-03-07 21:26:38 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | private int getInt(final String key, final int defaultValue) { |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1111 | if (mCopyOnWrite && mCopyOnWriteDataStore.containsKey(key)) { |
| 1112 | final String result = mCopyOnWriteDataStore.get(key); |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 1113 | return result != null ? Integer.parseInt(result) : 0; |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1114 | } |
Yohei Yukawa | f0ed48d | 2016-03-07 21:26:38 -0800 | [diff] [blame] | 1115 | return Settings.Secure.getIntForUser(mResolver, key, defaultValue, mCurrentUserId); |
| 1116 | } |
| 1117 | |
| 1118 | private void putBoolean(final String key, final boolean value) { |
| 1119 | putInt(key, value ? 1 : 0); |
| 1120 | } |
| 1121 | |
| 1122 | private boolean getBoolean(final String key, final boolean defaultValue) { |
| 1123 | return getInt(key, defaultValue ? 1 : 0) == 1; |
| 1124 | } |
| 1125 | |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 1126 | public void setCurrentProfileIds(int[] currentProfileIds) { |
| 1127 | synchronized (mLock) { |
| 1128 | mCurrentProfileIds = currentProfileIds; |
| 1129 | } |
| 1130 | } |
| 1131 | |
Yohei Yukawa | 9faa2ae | 2016-03-07 13:42:07 -0800 | [diff] [blame] | 1132 | public boolean isCurrentProfile(@UserIdInt int userId) { |
Yohei Yukawa | 095fa37 | 2014-10-27 14:02:23 +0900 | [diff] [blame] | 1133 | synchronized (mLock) { |
| 1134 | if (userId == mCurrentUserId) return true; |
| 1135 | for (int i = 0; i < mCurrentProfileIds.length; i++) { |
| 1136 | if (userId == mCurrentProfileIds[i]) return true; |
| 1137 | } |
| 1138 | return false; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Yohei Yukawa | 9faa2ae | 2016-03-07 13:42:07 -0800 | [diff] [blame] | 1142 | @UserIdInt |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1143 | public int getCurrentUserId() { |
| 1144 | return mCurrentUserId; |
| 1145 | } |
| 1146 | |
Yohei Yukawa | 08ce187 | 2016-03-16 17:22:30 -0700 | [diff] [blame] | 1147 | public void putSelectedSpellChecker(@Nullable String sciId) { |
| 1148 | if (TextUtils.isEmpty(sciId)) { |
| 1149 | // OK to coalesce to null, since getSelectedSpellChecker() can take care of the |
| 1150 | // empty data scenario. |
| 1151 | putString(Settings.Secure.SELECTED_SPELL_CHECKER, null); |
| 1152 | } else { |
| 1153 | putString(Settings.Secure.SELECTED_SPELL_CHECKER, sciId); |
| 1154 | } |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | public void putSelectedSpellCheckerSubtype(int hashCode) { |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 1158 | putInt(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, hashCode); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | public void setSpellCheckerEnabled(boolean enabled) { |
Yohei Yukawa | f0ed48d | 2016-03-07 21:26:38 -0800 | [diff] [blame] | 1162 | putBoolean(Settings.Secure.SPELL_CHECKER_ENABLED, enabled); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1163 | } |
| 1164 | |
Yohei Yukawa | 08ce187 | 2016-03-16 17:22:30 -0700 | [diff] [blame] | 1165 | @NonNull |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1166 | public String getSelectedSpellChecker() { |
Yohei Yukawa | 08ce187 | 2016-03-16 17:22:30 -0700 | [diff] [blame] | 1167 | return getString(Settings.Secure.SELECTED_SPELL_CHECKER, ""); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1168 | } |
| 1169 | |
Yohei Yukawa | ad150ee | 2016-03-16 17:22:27 -0700 | [diff] [blame] | 1170 | public int getSelectedSpellCheckerSubtype(final int defaultValue) { |
| 1171 | return getInt(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, defaultValue); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | public boolean isSpellCheckerEnabled() { |
Yohei Yukawa | f0ed48d | 2016-03-07 21:26:38 -0800 | [diff] [blame] | 1175 | return getBoolean(Settings.Secure.SPELL_CHECKER_ENABLED, true); |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1176 | } |
Yohei Yukawa | 85df698 | 2016-03-08 15:16:07 -0800 | [diff] [blame] | 1177 | |
| 1178 | public void dumpLocked(final PrintWriter pw, final String prefix) { |
| 1179 | pw.println(prefix + "mCurrentUserId=" + mCurrentUserId); |
| 1180 | pw.println(prefix + "mCurrentProfileIds=" + Arrays.toString(mCurrentProfileIds)); |
Yohei Yukawa | 49ed140 | 2016-03-07 19:12:54 -0800 | [diff] [blame] | 1181 | pw.println(prefix + "mCopyOnWrite=" + mCopyOnWrite); |
Yohei Yukawa | 85df698 | 2016-03-08 15:16:07 -0800 | [diff] [blame] | 1182 | } |
Satoshi Kataoka | 00d2d41 | 2012-09-28 20:32:33 +0900 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | // ---------------------------------------------------------------------- |
| 1186 | // Utilities for debug |
| 1187 | private static String getStackTrace() { |
| 1188 | final StringBuilder sb = new StringBuilder(); |
| 1189 | try { |
| 1190 | throw new RuntimeException(); |
| 1191 | } catch (RuntimeException e) { |
| 1192 | final StackTraceElement[] frames = e.getStackTrace(); |
| 1193 | // Start at 1 because the first frame is here and we don't care about it |
| 1194 | for (int j = 1; j < frames.length; ++j) { |
| 1195 | sb.append(frames[j].toString() + "\n"); |
| 1196 | } |
| 1197 | } |
| 1198 | return sb.toString(); |
| 1199 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1200 | } |