diff options
Diffstat (limited to 'core/java/android')
15 files changed, 265 insertions, 86 deletions
diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java index 71cb4a403365..cd05e2c948b1 100644 --- a/core/java/android/app/ApplicationPackageManager.java +++ b/core/java/android/app/ApplicationPackageManager.java @@ -3229,18 +3229,18 @@ public class ApplicationPackageManager extends PackageManager { } @Override - public String getSystemTextClassifierPackageName() { + public String getDefaultTextClassifierPackageName() { try { - return mPM.getSystemTextClassifierPackageName(); + return mPM.getDefaultTextClassifierPackageName(); } catch (RemoteException e) { throw e.rethrowAsRuntimeException(); } } @Override - public String[] getSystemTextClassifierPackages() { + public String getSystemTextClassifierPackageName() { try { - return mPM.getSystemTextClassifierPackages(); + return mPM.getSystemTextClassifierPackageName(); } catch (RemoteException e) { throw e.rethrowAsRuntimeException(); } diff --git a/core/java/android/content/pm/IPackageManager.aidl b/core/java/android/content/pm/IPackageManager.aidl index 93126b8002ad..6552d1b5a824 100644 --- a/core/java/android/content/pm/IPackageManager.aidl +++ b/core/java/android/content/pm/IPackageManager.aidl @@ -679,9 +679,9 @@ interface IPackageManager { boolean hasUidSigningCertificate(int uid, in byte[] signingCertificate, int flags); - String getSystemTextClassifierPackageName(); + String getDefaultTextClassifierPackageName(); - String[] getSystemTextClassifierPackages(); + String getSystemTextClassifierPackageName(); String getAttentionServicePackageName(); diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java index 6d5e8fb0240e..51d5c3f6725b 100644 --- a/core/java/android/content/pm/PackageManager.java +++ b/core/java/android/content/pm/PackageManager.java @@ -7602,14 +7602,15 @@ public abstract class PackageManager { } /** - * @return the system defined text classifier package name, or null if there's none. + * @return the default text classifier package name, or null if there's none. * * @hide */ @Nullable - public String getSystemTextClassifierPackageName() { + @TestApi + public String getDefaultTextClassifierPackageName() { throw new UnsupportedOperationException( - "getSystemTextClassifierPackageName not implemented in subclass"); + "getDefaultTextClassifierPackageName not implemented in subclass"); } /** @@ -7617,10 +7618,11 @@ public abstract class PackageManager { * * @hide */ - @NonNull - public String[] getSystemTextClassifierPackages() { + @Nullable + @TestApi + public String getSystemTextClassifierPackageName() { throw new UnsupportedOperationException( - "getSystemTextClassifierPackages not implemented in subclass"); + "getSystemTextClassifierPackageName not implemented in subclass"); } /** diff --git a/core/java/android/service/textclassifier/TextClassifierService.java b/core/java/android/service/textclassifier/TextClassifierService.java index 8dca69f856e5..848868a5532f 100644 --- a/core/java/android/service/textclassifier/TextClassifierService.java +++ b/core/java/android/service/textclassifier/TextClassifierService.java @@ -27,7 +27,6 @@ import android.app.Service; import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.pm.ServiceInfo; import android.os.Bundle; @@ -42,7 +41,6 @@ import android.util.Slog; import android.view.textclassifier.ConversationActions; import android.view.textclassifier.SelectionEvent; import android.view.textclassifier.TextClassification; -import android.view.textclassifier.TextClassificationConstants; import android.view.textclassifier.TextClassificationContext; import android.view.textclassifier.TextClassificationManager; import android.view.textclassifier.TextClassificationSessionId; @@ -394,19 +392,32 @@ public abstract class TextClassifierService extends Service { */ @Deprecated public final TextClassifier getLocalTextClassifier() { - // Deprecated: In the future, we may not guarantee that this runs in the service's process. - return getDefaultTextClassifierImplementation(this); + return TextClassifier.NO_OP; } /** * Returns the platform's default TextClassifier implementation. + * + * @throws RuntimeException if the TextClassifier from + * PackageManager#getDefaultTextClassifierPackageName() calls + * this method. */ @NonNull public static TextClassifier getDefaultTextClassifierImplementation(@NonNull Context context) { + final String defaultTextClassifierPackageName = + context.getPackageManager().getDefaultTextClassifierPackageName(); + if (TextUtils.isEmpty(defaultTextClassifierPackageName)) { + return TextClassifier.NO_OP; + } + if (defaultTextClassifierPackageName.equals(context.getPackageName())) { + throw new RuntimeException( + "The default text classifier itself should not call the" + + "getDefaultTextClassifierImplementation() method."); + } final TextClassificationManager tcm = context.getSystemService(TextClassificationManager.class); if (tcm != null) { - return tcm.getTextClassifier(TextClassifier.LOCAL); + return tcm.getTextClassifier(TextClassifier.DEFAULT_SERVICE); } return TextClassifier.NO_OP; } @@ -434,46 +445,20 @@ public abstract class TextClassifierService extends Service { } /** - * Returns the component name of the system default textclassifier service if it can be found - * on the system. Otherwise, returns null. + * Returns the component name of the textclassifier service from the given package. + * Otherwise, returns null. * - * @param context the text classification context + * @param context + * @param packageName the package to look for. + * @param resolveFlags the flags that are used by PackageManager to resolve the component name. * @hide */ @Nullable - public static ComponentName getServiceComponentName(@NonNull Context context) { - final TextClassificationConstants settings = TextClassificationManager.getSettings(context); - // get override TextClassifierService package name - String packageName = settings.getTextClassifierServicePackageOverride(); - - ComponentName serviceComponent = null; - final boolean isOverrideService = !TextUtils.isEmpty(packageName); - if (isOverrideService) { - serviceComponent = getServiceComponentNameByPackage(context, packageName, - isOverrideService); - } - if (serviceComponent != null) { - return serviceComponent; - } - // If no TextClassifierService override or invalid override package name, read the first - // package defined in the config - final String[] packages = context.getPackageManager().getSystemTextClassifierPackages(); - if (packages.length == 0 || TextUtils.isEmpty(packages[0])) { - Slog.d(LOG_TAG, "No configured system TextClassifierService"); - return null; - } - packageName = packages[0]; - serviceComponent = getServiceComponentNameByPackage(context, packageName, - isOverrideService); - return serviceComponent; - } - - private static ComponentName getServiceComponentNameByPackage(Context context, - String packageName, boolean isOverrideService) { + public static ComponentName getServiceComponentName( + Context context, String packageName, int resolveFlags) { final Intent intent = new Intent(SERVICE_INTERFACE).setPackage(packageName); - final int flags = isOverrideService ? 0 : PackageManager.MATCH_SYSTEM_ONLY; - final ResolveInfo ri = context.getPackageManager().resolveService(intent, flags); + final ResolveInfo ri = context.getPackageManager().resolveService(intent, resolveFlags); if ((ri == null) || (ri.serviceInfo == null)) { Slog.w(LOG_TAG, String.format("Package or service not found in package %s for user %d", diff --git a/core/java/android/view/textclassifier/ConversationActions.java b/core/java/android/view/textclassifier/ConversationActions.java index 80027b1ed75d..6246b507ba59 100644 --- a/core/java/android/view/textclassifier/ConversationActions.java +++ b/core/java/android/view/textclassifier/ConversationActions.java @@ -323,6 +323,7 @@ public final class ConversationActions implements Parcelable { private int mUserId = UserHandle.USER_NULL; @NonNull private Bundle mExtras; + private boolean mUseDefaultTextClassifier; private Request( @NonNull List<Message> conversation, @@ -347,6 +348,8 @@ public final class ConversationActions implements Parcelable { String callingPackageName = in.readString(); int userId = in.readInt(); Bundle extras = in.readBundle(); + boolean useDefaultTextClassifier = in.readBoolean(); + Request request = new Request( conversation, typeConfig, @@ -355,6 +358,7 @@ public final class ConversationActions implements Parcelable { extras); request.setCallingPackageName(callingPackageName); request.setUserId(userId); + request.setUseDefaultTextClassifier(useDefaultTextClassifier); return request; } @@ -367,6 +371,7 @@ public final class ConversationActions implements Parcelable { parcel.writeString(mCallingPackageName); parcel.writeInt(mUserId); parcel.writeBundle(mExtras); + parcel.writeBoolean(mUseDefaultTextClassifier); } @Override @@ -455,6 +460,26 @@ public final class ConversationActions implements Parcelable { } /** + * Sets whether to use the default text classifier to handle this request. + * This will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { + mUseDefaultTextClassifier = useDefaultTextClassifier; + } + + /** + * Returns whether to use the default text classifier to handle this request. This + * will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + public boolean getUseDefaultTextClassifier() { + return mUseDefaultTextClassifier; + } + + /** * Returns the extended data related to this request. * * <p><b>NOTE: </b>Do not modify this bundle. diff --git a/core/java/android/view/textclassifier/SelectionEvent.java b/core/java/android/view/textclassifier/SelectionEvent.java index 09cb7a07faa8..e0f29a981d89 100644 --- a/core/java/android/view/textclassifier/SelectionEvent.java +++ b/core/java/android/view/textclassifier/SelectionEvent.java @@ -140,6 +140,7 @@ public final class SelectionEvent implements Parcelable { private int mEnd; private int mSmartStart; private int mSmartEnd; + private boolean mUseDefaultTextClassifier; SelectionEvent( int start, int end, @@ -175,6 +176,7 @@ public final class SelectionEvent implements Parcelable { mSmartStart = in.readInt(); mSmartEnd = in.readInt(); mUserId = in.readInt(); + mUseDefaultTextClassifier = in.readBoolean(); } @Override @@ -204,6 +206,7 @@ public final class SelectionEvent implements Parcelable { dest.writeInt(mSmartStart); dest.writeInt(mSmartEnd); dest.writeInt(mUserId); + dest.writeBoolean(mUseDefaultTextClassifier); } @Override @@ -428,6 +431,26 @@ public final class SelectionEvent implements Parcelable { } /** + * Sets whether to use the default text classifier to handle this request. + * This will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { + mUseDefaultTextClassifier = useDefaultTextClassifier; + } + + /** + * Returns whether to use the default text classifier to handle this request. This + * will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + public boolean getUseDefaultTextClassifier() { + return mUseDefaultTextClassifier; + } + + /** * Returns the type of widget that was involved in triggering this event. */ @WidgetType @@ -642,7 +665,8 @@ public final class SelectionEvent implements Parcelable { return Objects.hash(mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, mWidgetVersion, mPackageName, mUserId, mWidgetType, mInvocationMethod, mResultId, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent, - mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd); + mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, + mUseDefaultTextClassifier); } @Override @@ -673,7 +697,8 @@ public final class SelectionEvent implements Parcelable { && mStart == other.mStart && mEnd == other.mEnd && mSmartStart == other.mSmartStart - && mSmartEnd == other.mSmartEnd; + && mSmartEnd == other.mSmartEnd + && mUseDefaultTextClassifier == other.mUseDefaultTextClassifier; } @Override @@ -683,12 +708,13 @@ public final class SelectionEvent implements Parcelable { + "widgetVersion=%s, packageName=%s, widgetType=%s, invocationMethod=%s, " + "userId=%d, resultId=%s, eventTime=%d, durationSinceSessionStart=%d, " + "durationSincePreviousEvent=%d, eventIndex=%d," - + "sessionId=%s, start=%d, end=%d, smartStart=%d, smartEnd=%d}", + + "sessionId=%s, start=%d, end=%d, smartStart=%d, smartEnd=%d, " + + "mUseDefaultTextClassifier=%b}", mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod, mUserId, mResultId, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent, mEventIndex, - mSessionId, mStart, mEnd, mSmartStart, mSmartEnd); + mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mUseDefaultTextClassifier); } public static final @android.annotation.NonNull Creator<SelectionEvent> CREATOR = new Creator<SelectionEvent>() { diff --git a/core/java/android/view/textclassifier/SystemTextClassifier.java b/core/java/android/view/textclassifier/SystemTextClassifier.java index 138d25dd7c83..fe5e8d658dc3 100644 --- a/core/java/android/view/textclassifier/SystemTextClassifier.java +++ b/core/java/android/view/textclassifier/SystemTextClassifier.java @@ -55,17 +55,20 @@ public final class SystemTextClassifier implements TextClassifier { // service will throw a remote exception. @UserIdInt private final int mUserId; + private final boolean mUseDefault; private TextClassificationSessionId mSessionId; - public SystemTextClassifier(Context context, TextClassificationConstants settings) - throws ServiceManager.ServiceNotFoundException { + public SystemTextClassifier( + Context context, + TextClassificationConstants settings, + boolean useDefault) throws ServiceManager.ServiceNotFoundException { mManagerService = ITextClassifierService.Stub.asInterface( ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE)); mSettings = Objects.requireNonNull(settings); - mFallback = context.getSystemService(TextClassificationManager.class) - .getTextClassifier(TextClassifier.LOCAL); + mFallback = TextClassifier.NO_OP; mPackageName = Objects.requireNonNull(context.getOpPackageName()); mUserId = context.getUserId(); + mUseDefault = useDefault; } /** @@ -79,6 +82,7 @@ public final class SystemTextClassifier implements TextClassifier { try { request.setCallingPackageName(mPackageName); request.setUserId(mUserId); + request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextSelection> callback = new BlockingCallback<>("textselection"); mManagerService.onSuggestSelection(mSessionId, request, callback); @@ -103,6 +107,7 @@ public final class SystemTextClassifier implements TextClassifier { try { request.setCallingPackageName(mPackageName); request.setUserId(mUserId); + request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextClassification> callback = new BlockingCallback<>("textclassification"); mManagerService.onClassifyText(mSessionId, request, callback); @@ -124,7 +129,9 @@ public final class SystemTextClassifier implements TextClassifier { public TextLinks generateLinks(@NonNull TextLinks.Request request) { Objects.requireNonNull(request); Utils.checkMainThread(); - + if (!Utils.checkTextLength(request.getText(), getMaxGenerateLinksTextLength())) { + return mFallback.generateLinks(request); + } if (!mSettings.isSmartLinkifyEnabled() && request.isLegacyFallback()) { return Utils.generateLegacyLinks(request); } @@ -132,6 +139,7 @@ public final class SystemTextClassifier implements TextClassifier { try { request.setCallingPackageName(mPackageName); request.setUserId(mUserId); + request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextLinks> callback = new BlockingCallback<>("textlinks"); mManagerService.onGenerateLinks(mSessionId, request, callback); @@ -152,6 +160,7 @@ public final class SystemTextClassifier implements TextClassifier { try { event.setUserId(mUserId); + event.setUseDefaultTextClassifier(mUseDefault); mManagerService.onSelectionEvent(mSessionId, event); } catch (RemoteException e) { Log.e(LOG_TAG, "Error reporting selection event.", e); @@ -169,6 +178,7 @@ public final class SystemTextClassifier implements TextClassifier { .build() : event.getEventContext(); tcContext.setUserId(mUserId); + tcContext.setUseDefaultTextClassifier(mUseDefault); event.setEventContext(tcContext); mManagerService.onTextClassifierEvent(mSessionId, event); } catch (RemoteException e) { @@ -184,6 +194,7 @@ public final class SystemTextClassifier implements TextClassifier { try { request.setCallingPackageName(mPackageName); request.setUserId(mUserId); + request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextLanguage> callback = new BlockingCallback<>("textlanguage"); mManagerService.onDetectLanguage(mSessionId, request, callback); @@ -205,6 +216,7 @@ public final class SystemTextClassifier implements TextClassifier { try { request.setCallingPackageName(mPackageName); request.setUserId(mUserId); + request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<ConversationActions> callback = new BlockingCallback<>("conversation-actions"); mManagerService.onSuggestConversationActions(mSessionId, request, callback); @@ -225,7 +237,7 @@ public final class SystemTextClassifier implements TextClassifier { @WorkerThread public int getMaxGenerateLinksTextLength() { // TODO: retrieve this from the bound service. - return mFallback.getMaxGenerateLinksTextLength(); + return mSettings.getGenerateLinksMaxTextLength(); } @Override @@ -247,6 +259,7 @@ public final class SystemTextClassifier implements TextClassifier { printWriter.printPair("mPackageName", mPackageName); printWriter.printPair("mSessionId", mSessionId); printWriter.printPair("mUserId", mUserId); + printWriter.printPair("mUseDefault", mUseDefault); printWriter.decreaseIndent(); printWriter.println(); } diff --git a/core/java/android/view/textclassifier/TextClassification.java b/core/java/android/view/textclassifier/TextClassification.java index 3628d2d40c1e..00f762b44a07 100644 --- a/core/java/android/view/textclassifier/TextClassification.java +++ b/core/java/android/view/textclassifier/TextClassification.java @@ -555,6 +555,7 @@ public final class TextClassification implements Parcelable { @Nullable private String mCallingPackageName; @UserIdInt private int mUserId = UserHandle.USER_NULL; + private boolean mUseDefaultTextClassifier; private Request( CharSequence text, @@ -654,6 +655,26 @@ public final class TextClassification implements Parcelable { } /** + * Sets whether to use the default text classifier to handle this request. + * This will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { + mUseDefaultTextClassifier = useDefaultTextClassifier; + } + + /** + * Returns whether to use the default text classifier to handle this request. This + * will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + public boolean getUseDefaultTextClassifier() { + return mUseDefaultTextClassifier; + } + + /** * Returns the extended data. * * <p><b>NOTE: </b>Do not modify this bundle. @@ -755,6 +776,7 @@ public final class TextClassification implements Parcelable { dest.writeString(mCallingPackageName); dest.writeInt(mUserId); dest.writeBundle(mExtras); + dest.writeBoolean(mUseDefaultTextClassifier); } private static Request readFromParcel(Parcel in) { @@ -768,11 +790,13 @@ public final class TextClassification implements Parcelable { final String callingPackageName = in.readString(); final int userId = in.readInt(); final Bundle extras = in.readBundle(); + final boolean useDefaultTextClassifier = in.readBoolean(); final Request request = new Request(text, startIndex, endIndex, defaultLocales, referenceTime, extras); request.setCallingPackageName(callingPackageName); request.setUserId(userId); + request.setUseDefaultTextClassifier(useDefaultTextClassifier); return request; } diff --git a/core/java/android/view/textclassifier/TextClassificationContext.java b/core/java/android/view/textclassifier/TextClassificationContext.java index 930765b29197..d58d175c9c93 100644 --- a/core/java/android/view/textclassifier/TextClassificationContext.java +++ b/core/java/android/view/textclassifier/TextClassificationContext.java @@ -38,6 +38,7 @@ public final class TextClassificationContext implements Parcelable { @Nullable private final String mWidgetVersion; @UserIdInt private int mUserId = UserHandle.USER_NULL; + private boolean mUseDefaultTextClassifier; private TextClassificationContext( String packageName, @@ -76,6 +77,26 @@ public final class TextClassificationContext implements Parcelable { } /** + * Sets whether to use the default text classifier to handle this request. + * This will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { + mUseDefaultTextClassifier = useDefaultTextClassifier; + } + + /** + * Returns whether to use the default text classifier to handle this request. This + * will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + public boolean getUseDefaultTextClassifier() { + return mUseDefaultTextClassifier; + } + + /** * Returns the widget type for this classification context. */ @NonNull @@ -156,6 +177,7 @@ public final class TextClassificationContext implements Parcelable { parcel.writeString(mWidgetType); parcel.writeString(mWidgetVersion); parcel.writeInt(mUserId); + parcel.writeBoolean(mUseDefaultTextClassifier); } private TextClassificationContext(Parcel in) { @@ -163,6 +185,7 @@ public final class TextClassificationContext implements Parcelable { mWidgetType = in.readString(); mWidgetVersion = in.readString(); mUserId = in.readInt(); + mUseDefaultTextClassifier = in.readBoolean(); } public static final @android.annotation.NonNull Parcelable.Creator<TextClassificationContext> CREATOR = diff --git a/core/java/android/view/textclassifier/TextClassificationManager.java b/core/java/android/view/textclassifier/TextClassificationManager.java index bb96d5543b0b..a6c83a1cfa76 100644 --- a/core/java/android/view/textclassifier/TextClassificationManager.java +++ b/core/java/android/view/textclassifier/TextClassificationManager.java @@ -25,7 +25,7 @@ import android.content.Context; import android.os.ServiceManager; import android.provider.DeviceConfig; import android.provider.DeviceConfig.Properties; -import android.service.textclassifier.TextClassifierService; +import android.util.SparseArray; import android.view.textclassifier.TextClassifier.TextClassifierType; import com.android.internal.annotations.GuardedBy; @@ -62,8 +62,7 @@ public final class TextClassificationManager { @Nullable private TextClassifier mLocalTextClassifier; @GuardedBy("mLock") - @Nullable - private TextClassifier mSystemTextClassifier; + private SparseArray<TextClassifier> mSystemTextClassifiers = new SparseArray<>(); @GuardedBy("mLock") private TextClassificationSessionFactory mSessionFactory; @GuardedBy("mLock") @@ -91,8 +90,8 @@ public final class TextClassificationManager { synchronized (mLock) { if (mCustomTextClassifier != null) { return mCustomTextClassifier; - } else if (isSystemTextClassifierEnabled()) { - return getSystemTextClassifier(); + } else if (getSettings().isSystemTextClassifierEnabled()) { + return getSystemTextClassifier(SystemTextClassifier.SYSTEM); } else { return getLocalTextClassifier(); } @@ -116,6 +115,7 @@ public final class TextClassificationManager { * * @see TextClassifier#LOCAL * @see TextClassifier#SYSTEM + * @see TextClassifier#DEFAULT_SERVICE * @hide */ @UnsupportedAppUsage @@ -124,7 +124,7 @@ public final class TextClassificationManager { case TextClassifier.LOCAL: return getLocalTextClassifier(); default: - return getSystemTextClassifier(); + return getSystemTextClassifier(type); } } @@ -204,21 +204,28 @@ public final class TextClassificationManager { } } - private TextClassifier getSystemTextClassifier() { + /** @hide */ + private TextClassifier getSystemTextClassifier(@TextClassifierType int type) { synchronized (mLock) { - if (mSystemTextClassifier == null && isSystemTextClassifierEnabled()) { + if (mSystemTextClassifiers.get(type) == null + && getSettings().isSystemTextClassifierEnabled()) { try { - mSystemTextClassifier = new SystemTextClassifier(mContext, getSettings()); - Log.d(LOG_TAG, "Initialized SystemTextClassifier"); + mSystemTextClassifiers.put( + type, + new SystemTextClassifier( + mContext, + getSettings(), + /* useDefault= */ type == TextClassifier.DEFAULT_SERVICE)); + Log.d(LOG_TAG, "Initialized SystemTextClassifier, type = " + type); } catch (ServiceManager.ServiceNotFoundException e) { Log.e(LOG_TAG, "Could not initialize SystemTextClassifier", e); } } + if (mSystemTextClassifiers.get(type) != null) { + return mSystemTextClassifiers.get(type); + } + return TextClassifier.NO_OP; } - if (mSystemTextClassifier != null) { - return mSystemTextClassifier; - } - return TextClassifier.NO_OP; } /** @@ -240,11 +247,6 @@ public final class TextClassificationManager { } } - private boolean isSystemTextClassifierEnabled() { - return getSettings().isSystemTextClassifierEnabled() - && TextClassifierService.getServiceComponentName(mContext) != null; - } - /** @hide */ @VisibleForTesting public void invalidateForTesting() { @@ -261,7 +263,7 @@ public final class TextClassificationManager { private void invalidateTextClassifiers() { synchronized (mLock) { mLocalTextClassifier = null; - mSystemTextClassifier = null; + mSystemTextClassifiers.clear(); } } @@ -274,7 +276,8 @@ public final class TextClassificationManager { /** @hide **/ public void dump(IndentingPrintWriter pw) { getLocalTextClassifier().dump(pw); - getSystemTextClassifier().dump(pw); + getSystemTextClassifier(TextClassifier.DEFAULT_SERVICE).dump(pw); + getSystemTextClassifier(TextClassifier.SYSTEM).dump(pw); getSettings().dump(pw); } diff --git a/core/java/android/view/textclassifier/TextClassifier.java b/core/java/android/view/textclassifier/TextClassifier.java index 9b3369390b6a..2cc226d5a601 100644 --- a/core/java/android/view/textclassifier/TextClassifier.java +++ b/core/java/android/view/textclassifier/TextClassifier.java @@ -66,12 +66,14 @@ public interface TextClassifier { /** @hide */ @Retention(RetentionPolicy.SOURCE) - @IntDef(value = {LOCAL, SYSTEM}) + @IntDef(value = {LOCAL, SYSTEM, DEFAULT_SERVICE}) @interface TextClassifierType {} // TODO: Expose as system APIs. /** Specifies a TextClassifier that runs locally in the app's process. @hide */ int LOCAL = 0; /** Specifies a TextClassifier that runs in the system process and serves all apps. @hide */ int SYSTEM = 1; + /** Specifies the default TextClassifier that runs in the system process. @hide */ + int DEFAULT_SERVICE = 2; /** The TextClassifier failed to run. */ String TYPE_UNKNOWN = ""; @@ -667,8 +669,10 @@ public interface TextClassifier { Preconditions.checkArgument(endIndex > startIndex); } - static void checkTextLength(CharSequence text, int maxLength) { - Preconditions.checkArgumentInRange(text.length(), 0, maxLength, "text.length()"); + /** Returns if the length of the text is within the range. */ + static boolean checkTextLength(CharSequence text, int maxLength) { + int textLength = text.length(); + return textLength >= 0 && textLength <= maxLength; } /** diff --git a/core/java/android/view/textclassifier/TextClassifierImpl.java b/core/java/android/view/textclassifier/TextClassifierImpl.java index 61bd7c72b80b..d7149ee05b57 100644 --- a/core/java/android/view/textclassifier/TextClassifierImpl.java +++ b/core/java/android/view/textclassifier/TextClassifierImpl.java @@ -286,8 +286,10 @@ public final class TextClassifierImpl implements TextClassifier { @WorkerThread public TextLinks generateLinks(@NonNull TextLinks.Request request) { Objects.requireNonNull(request); - Utils.checkTextLength(request.getText(), getMaxGenerateLinksTextLength()); Utils.checkMainThread(); + if (!Utils.checkTextLength(request.getText(), getMaxGenerateLinksTextLength())) { + return mFallback.generateLinks(request); + } if (!mSettings.isSmartLinkifyEnabled() && request.isLegacyFallback()) { return Utils.generateLegacyLinks(request); diff --git a/core/java/android/view/textclassifier/TextLanguage.java b/core/java/android/view/textclassifier/TextLanguage.java index cc9109e6e9bb..58024dcc09b9 100644 --- a/core/java/android/view/textclassifier/TextLanguage.java +++ b/core/java/android/view/textclassifier/TextLanguage.java @@ -230,6 +230,7 @@ public final class TextLanguage implements Parcelable { @Nullable private String mCallingPackageName; @UserIdInt private int mUserId = UserHandle.USER_NULL; + private boolean mUseDefaultTextClassifier; private Request(CharSequence text, Bundle bundle) { mText = text; @@ -283,6 +284,26 @@ public final class TextLanguage implements Parcelable { } /** + * Sets whether to use the default text classifier to handle this request. + * This will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { + mUseDefaultTextClassifier = useDefaultTextClassifier; + } + + /** + * Returns whether to use the default text classifier to handle this request. This + * will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + public boolean getUseDefaultTextClassifier() { + return mUseDefaultTextClassifier; + } + + /** * Returns a bundle containing non-structured extra information about this request. * * <p><b>NOTE: </b>Do not modify this bundle. @@ -303,6 +324,7 @@ public final class TextLanguage implements Parcelable { dest.writeString(mCallingPackageName); dest.writeInt(mUserId); dest.writeBundle(mExtra); + dest.writeBoolean(mUseDefaultTextClassifier); } private static Request readFromParcel(Parcel in) { @@ -310,10 +332,12 @@ public final class TextLanguage implements Parcelable { final String callingPackageName = in.readString(); final int userId = in.readInt(); final Bundle extra = in.readBundle(); + final boolean useDefaultTextClassifier = in.readBoolean(); final Request request = new Request(text, extra); request.setCallingPackageName(callingPackageName); request.setUserId(userId); + request.setUseDefaultTextClassifier(useDefaultTextClassifier); return request; } diff --git a/core/java/android/view/textclassifier/TextLinks.java b/core/java/android/view/textclassifier/TextLinks.java index bda12b0893d1..7430cb38b987 100644 --- a/core/java/android/view/textclassifier/TextLinks.java +++ b/core/java/android/view/textclassifier/TextLinks.java @@ -345,6 +345,7 @@ public final class TextLinks implements Parcelable { @Nullable private final ZonedDateTime mReferenceTime; @UserIdInt private int mUserId = UserHandle.USER_NULL; + private boolean mUseDefaultTextClassifier; private Request( CharSequence text, @@ -447,6 +448,26 @@ public final class TextLinks implements Parcelable { } /** + * Sets whether to use the default text classifier to handle this request. + * This will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { + mUseDefaultTextClassifier = useDefaultTextClassifier; + } + + /** + * Returns whether to use the default text classifier to handle this request. This + * will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + public boolean getUseDefaultTextClassifier() { + return mUseDefaultTextClassifier; + } + + /** * Returns the extended data. * * <p><b>NOTE: </b>Do not modify this bundle. @@ -568,6 +589,7 @@ public final class TextLinks implements Parcelable { dest.writeInt(mUserId); dest.writeBundle(mExtras); dest.writeString(mReferenceTime == null ? null : mReferenceTime.toString()); + dest.writeBoolean(mUseDefaultTextClassifier); } private static Request readFromParcel(Parcel in) { @@ -580,11 +602,13 @@ public final class TextLinks implements Parcelable { final String referenceTimeString = in.readString(); final ZonedDateTime referenceTime = referenceTimeString == null ? null : ZonedDateTime.parse(referenceTimeString); + final boolean useDefaultTextClassifier = in.readBoolean(); final Request request = new Request(text, defaultLocales, entityConfig, /* legacyFallback= */ true, referenceTime, extras); request.setCallingPackageName(callingPackageName); request.setUserId(userId); + request.setUseDefaultTextClassifier(useDefaultTextClassifier); return request; } diff --git a/core/java/android/view/textclassifier/TextSelection.java b/core/java/android/view/textclassifier/TextSelection.java index 4a36cbfc35e5..575a072d7066 100644 --- a/core/java/android/view/textclassifier/TextSelection.java +++ b/core/java/android/view/textclassifier/TextSelection.java @@ -216,6 +216,7 @@ public final class TextSelection implements Parcelable { @Nullable private String mCallingPackageName; @UserIdInt private int mUserId = UserHandle.USER_NULL; + private boolean mUseDefaultTextClassifier; private Request( CharSequence text, @@ -316,6 +317,26 @@ public final class TextSelection implements Parcelable { } /** + * Sets whether to use the default text classifier to handle this request. + * This will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { + mUseDefaultTextClassifier = useDefaultTextClassifier; + } + + /** + * Returns whether to use the default text classifier to handle this request. This + * will be ignored if it is not the system text classifier to handle this request. + * + * @hide + */ + public boolean getUseDefaultTextClassifier() { + return mUseDefaultTextClassifier; + } + + /** * Returns the extended data. * * <p><b>NOTE: </b>Do not modify this bundle. @@ -420,6 +441,7 @@ public final class TextSelection implements Parcelable { dest.writeString(mCallingPackageName); dest.writeInt(mUserId); dest.writeBundle(mExtras); + dest.writeBoolean(mUseDefaultTextClassifier); } private static Request readFromParcel(Parcel in) { @@ -430,11 +452,13 @@ public final class TextSelection implements Parcelable { final String callingPackageName = in.readString(); final int userId = in.readInt(); final Bundle extras = in.readBundle(); + final boolean systemTextClassifierType = in.readBoolean(); final Request request = new Request(text, startIndex, endIndex, defaultLocales, /* darkLaunchAllowed= */ false, extras); request.setCallingPackageName(callingPackageName); request.setUserId(userId); + request.setUseDefaultTextClassifier(systemTextClassifierType); return request; } |
