| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.textservice; |
| 18 |
|
| 19 |
import android.annotation.CallbackExecutor; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SystemService; |
| 23 |
import android.annotation.UserHandleAware; |
| 24 |
import android.annotation.UserIdInt; |
| 25 |
import android.compat.annotation.UnsupportedAppUsage; |
| 26 |
import android.content.Context; |
| 27 |
import android.os.Build; |
| 28 |
import android.os.Bundle; |
| 29 |
import android.os.Handler; |
| 30 |
import android.os.HandlerExecutor; |
| 31 |
import android.os.RemoteException; |
| 32 |
import android.os.ServiceManager; |
| 33 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 34 |
import android.os.UserHandle; |
| 35 |
import android.util.Log; |
| 36 |
import android.view.inputmethod.InputMethodManager; |
| 37 |
import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener; |
| 38 |
import android.view.textservice.SpellCheckerSession.SpellCheckerSessionParams; |
| 39 |
|
| 40 |
import com.android.internal.textservice.ISpellCheckerSessionListener; |
| 41 |
import com.android.internal.textservice.ITextServicesManager; |
| 42 |
|
| 43 |
import java.util.Arrays; |
| 44 |
import java.util.Collections; |
| 45 |
import java.util.List; |
| 46 |
import java.util.Locale; |
| 47 |
import java.util.Objects; |
| 48 |
import java.util.concurrent.Executor; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
@SystemService(Context.TEXT_SERVICES_MANAGER_SERVICE) |
| 83 |
public final class TextServicesManager { |
| 84 |
private static final String TAG = TextServicesManager.class.getSimpleName(); |
| 85 |
private static final boolean DBG = false; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
@Deprecated |
| 92 |
private static TextServicesManager sInstance; |
| 93 |
|
| 94 |
private final ITextServicesManager mService; |
| 95 |
|
| 96 |
@UserIdInt |
| 97 |
private final int mUserId; |
| 98 |
|
| 99 |
@Nullable |
| 100 |
private final InputMethodManager mInputMethodManager; |
| 101 |
|
| 102 |
private TextServicesManager(@UserIdInt int userId, |
| 103 |
@Nullable InputMethodManager inputMethodManager) throws ServiceNotFoundException { |
| 104 |
mService = ITextServicesManager.Stub.asInterface( |
| 105 |
ServiceManager.getServiceOrThrow(Context.TEXT_SERVICES_MANAGER_SERVICE)); |
| 106 |
mUserId = userId; |
| 107 |
mInputMethodManager = inputMethodManager; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
@UserHandleAware |
| 119 |
@NonNull |
| 120 |
public static TextServicesManager createInstance(@NonNull Context context) |
| 121 |
throws ServiceNotFoundException { |
| 122 |
return new TextServicesManager(context.getUserId(), context.getSystemService( |
| 123 |
InputMethodManager.class)); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@UnsupportedAppUsage |
| 132 |
public static TextServicesManager getInstance() { |
| 133 |
synchronized (TextServicesManager.class) { |
| 134 |
if (sInstance == null) { |
| 135 |
try { |
| 136 |
sInstance = new TextServicesManager(UserHandle.myUserId(), null); |
| 137 |
} catch (ServiceNotFoundException e) { |
| 138 |
throw new IllegalStateException(e); |
| 139 |
} |
| 140 |
} |
| 141 |
return sInstance; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
@Nullable |
| 147 |
public InputMethodManager getInputMethodManager() { |
| 148 |
return mInputMethodManager; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private static String parseLanguageFromLocaleString(String locale) { |
| 155 |
final int idx = locale.indexOf('_'); |
| 156 |
if (idx < 0) { |
| 157 |
return locale; |
| 158 |
} else { |
| 159 |
return locale.substring(0, idx); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
@UserHandleAware |
| 184 |
@Nullable |
| 185 |
public SpellCheckerSession newSpellCheckerSession(@Nullable Bundle bundle, |
| 186 |
@Nullable Locale locale, |
| 187 |
@NonNull SpellCheckerSessionListener listener, |
| 188 |
boolean referToSpellCheckerLanguageSettings) { |
| 189 |
|
| 190 |
|
| 191 |
int supportedAttributes = SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY |
| 192 |
| SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO |
| 193 |
| SuggestionsInfo.RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS; |
| 194 |
SpellCheckerSessionParams.Builder paramsBuilder = new SpellCheckerSessionParams.Builder() |
| 195 |
.setLocale(locale) |
| 196 |
.setShouldReferToSpellCheckerLanguageSettings(referToSpellCheckerLanguageSettings) |
| 197 |
.setSupportedAttributes(supportedAttributes); |
| 198 |
if (bundle != null) { |
| 199 |
paramsBuilder.setExtras(bundle); |
| 200 |
} |
| 201 |
|
| 202 |
Executor executor = new HandlerExecutor(new Handler()); |
| 203 |
return newSpellCheckerSession(paramsBuilder.build(), executor, listener); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
@UserHandleAware |
| 215 |
@Nullable |
| 216 |
public SpellCheckerSession newSpellCheckerSession( |
| 217 |
@NonNull SpellCheckerSessionParams params, |
| 218 |
@NonNull @CallbackExecutor Executor executor, |
| 219 |
@NonNull SpellCheckerSessionListener listener) { |
| 220 |
Objects.requireNonNull(executor); |
| 221 |
Objects.requireNonNull(listener); |
| 222 |
Locale locale = params.getLocale(); |
| 223 |
if (!params.shouldReferToSpellCheckerLanguageSettings() && locale == null) { |
| 224 |
throw new IllegalArgumentException("Locale should not be null if you don't refer" |
| 225 |
+ " settings."); |
| 226 |
} |
| 227 |
|
| 228 |
if (params.shouldReferToSpellCheckerLanguageSettings() && !isSpellCheckerEnabled()) { |
| 229 |
return null; |
| 230 |
} |
| 231 |
|
| 232 |
final SpellCheckerInfo sci; |
| 233 |
try { |
| 234 |
sci = mService.getCurrentSpellChecker(mUserId, null); |
| 235 |
} catch (RemoteException e) { |
| 236 |
return null; |
| 237 |
} |
| 238 |
if (sci == null) { |
| 239 |
return null; |
| 240 |
} |
| 241 |
SpellCheckerSubtype subtypeInUse = null; |
| 242 |
if (params.shouldReferToSpellCheckerLanguageSettings()) { |
| 243 |
subtypeInUse = getCurrentSpellCheckerSubtype(true); |
| 244 |
if (subtypeInUse == null) { |
| 245 |
return null; |
| 246 |
} |
| 247 |
if (locale != null) { |
| 248 |
final String subtypeLocale = subtypeInUse.getLocale(); |
| 249 |
final String subtypeLanguage = parseLanguageFromLocaleString(subtypeLocale); |
| 250 |
if (subtypeLanguage.length() < 2 || !locale.getLanguage().equals(subtypeLanguage)) { |
| 251 |
return null; |
| 252 |
} |
| 253 |
} |
| 254 |
} else { |
| 255 |
final String localeStr = locale.toString(); |
| 256 |
for (int i = 0; i < sci.getSubtypeCount(); ++i) { |
| 257 |
final SpellCheckerSubtype subtype = sci.getSubtypeAt(i); |
| 258 |
final String tempSubtypeLocale = subtype.getLocale(); |
| 259 |
final String tempSubtypeLanguage = parseLanguageFromLocaleString(tempSubtypeLocale); |
| 260 |
if (tempSubtypeLocale.equals(localeStr)) { |
| 261 |
subtypeInUse = subtype; |
| 262 |
break; |
| 263 |
} else if (tempSubtypeLanguage.length() >= 2 && |
| 264 |
locale.getLanguage().equals(tempSubtypeLanguage)) { |
| 265 |
subtypeInUse = subtype; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
if (subtypeInUse == null) { |
| 270 |
return null; |
| 271 |
} |
| 272 |
final SpellCheckerSession session = new SpellCheckerSession(sci, this, listener, executor); |
| 273 |
try { |
| 274 |
mService.getSpellCheckerService(mUserId, sci.getId(), subtypeInUse.getLocale(), |
| 275 |
session.getTextServicesSessionListener(), |
| 276 |
session.getSpellCheckerSessionListener(), |
| 277 |
params.getExtras(), params.getSupportedAttributes()); |
| 278 |
} catch (RemoteException e) { |
| 279 |
throw e.rethrowFromSystemServer(); |
| 280 |
} |
| 281 |
return session; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553, |
| 289 |
publicAlternatives = "Use {@link #getEnabledSpellCheckerInfos()} instead.") |
| 290 |
@UserHandleAware |
| 291 |
public SpellCheckerInfo[] getEnabledSpellCheckers() { |
| 292 |
try { |
| 293 |
final SpellCheckerInfo[] retval = mService.getEnabledSpellCheckers(mUserId); |
| 294 |
if (DBG) { |
| 295 |
Log.d(TAG, "getEnabledSpellCheckers: " + (retval != null ? retval.length : "null")); |
| 296 |
} |
| 297 |
return retval; |
| 298 |
} catch (RemoteException e) { |
| 299 |
throw e.rethrowFromSystemServer(); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
@UserHandleAware |
| 309 |
@NonNull |
| 310 |
public List<SpellCheckerInfo> getEnabledSpellCheckerInfos() { |
| 311 |
final SpellCheckerInfo[] enabledSpellCheckers = getEnabledSpellCheckers(); |
| 312 |
return enabledSpellCheckers != null |
| 313 |
? Arrays.asList(enabledSpellCheckers) : Collections.emptyList(); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
@UserHandleAware |
| 322 |
@Nullable |
| 323 |
public SpellCheckerInfo getCurrentSpellCheckerInfo() { |
| 324 |
try { |
| 325 |
|
| 326 |
return mService.getCurrentSpellChecker(mUserId, null); |
| 327 |
} catch (RemoteException e) { |
| 328 |
throw e.rethrowFromSystemServer(); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, |
| 337 |
publicAlternatives = "Use {@link #getCurrentSpellCheckerInfo()} instead.") |
| 338 |
@UserHandleAware |
| 339 |
@Nullable |
| 340 |
public SpellCheckerInfo getCurrentSpellChecker() { |
| 341 |
return getCurrentSpellCheckerInfo(); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
@UnsupportedAppUsage |
| 354 |
@UserHandleAware |
| 355 |
@Nullable |
| 356 |
public SpellCheckerSubtype getCurrentSpellCheckerSubtype( |
| 357 |
boolean allowImplicitlySelectedSubtype) { |
| 358 |
try { |
| 359 |
return mService.getCurrentSpellCheckerSubtype(mUserId, allowImplicitlySelectedSubtype); |
| 360 |
} catch (RemoteException e) { |
| 361 |
throw e.rethrowFromSystemServer(); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
@UserHandleAware |
| 371 |
public boolean isSpellCheckerEnabled() { |
| 372 |
try { |
| 373 |
return mService.isSpellCheckerEnabled(mUserId); |
| 374 |
} catch (RemoteException e) { |
| 375 |
throw e.rethrowFromSystemServer(); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
@UserHandleAware |
| 380 |
void finishSpellCheckerService(ISpellCheckerSessionListener listener) { |
| 381 |
try { |
| 382 |
mService.finishSpellCheckerService(mUserId, listener); |
| 383 |
} catch (RemoteException e) { |
| 384 |
throw e.rethrowFromSystemServer(); |
| 385 |
} |
| 386 |
} |
| 387 |
} |