Merge "Fix TCS crash due to API changes." into pi-dev
am: d6997b05e0
Change-Id: Ieee28f1623704fc20925820b19a502da9ea27510
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package android.service.textclassifier;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.IntRange;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
@@ -97,7 +98,8 @@ public abstract class TextClassifierService extends Service {
|
||||
Preconditions.checkNotNull(request);
|
||||
Preconditions.checkNotNull(callback);
|
||||
TextClassifierService.this.onSuggestSelection(
|
||||
sessionId, request, mCancellationSignal,
|
||||
request.getText(), request.getStartIndex(), request.getEndIndex(),
|
||||
TextSelection.Options.from(sessionId, request), mCancellationSignal,
|
||||
new Callback<TextSelection>() {
|
||||
@Override
|
||||
public void onSuccess(TextSelection result) {
|
||||
@@ -130,7 +132,8 @@ public abstract class TextClassifierService extends Service {
|
||||
Preconditions.checkNotNull(request);
|
||||
Preconditions.checkNotNull(callback);
|
||||
TextClassifierService.this.onClassifyText(
|
||||
sessionId, request, mCancellationSignal,
|
||||
request.getText(), request.getStartIndex(), request.getEndIndex(),
|
||||
TextClassification.Options.from(sessionId, request), mCancellationSignal,
|
||||
new Callback<TextClassification>() {
|
||||
@Override
|
||||
public void onSuccess(TextClassification result) {
|
||||
@@ -161,7 +164,8 @@ public abstract class TextClassifierService extends Service {
|
||||
Preconditions.checkNotNull(request);
|
||||
Preconditions.checkNotNull(callback);
|
||||
TextClassifierService.this.onGenerateLinks(
|
||||
sessionId, request, mCancellationSignal,
|
||||
request.getText(), TextLinks.Options.from(sessionId, request),
|
||||
mCancellationSignal,
|
||||
new Callback<TextLinks>() {
|
||||
@Override
|
||||
public void onSuccess(TextLinks result) {
|
||||
@@ -234,6 +238,25 @@ public abstract class TextClassifierService extends Service {
|
||||
@NonNull CancellationSignal cancellationSignal,
|
||||
@NonNull Callback<TextSelection> callback);
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/** @hide */
|
||||
public void onSuggestSelection(
|
||||
@NonNull CharSequence text,
|
||||
@IntRange(from = 0) int selectionStartIndex,
|
||||
@IntRange(from = 0) int selectionEndIndex,
|
||||
@Nullable TextSelection.Options options,
|
||||
@NonNull CancellationSignal cancellationSignal,
|
||||
@NonNull Callback<TextSelection> callback) {
|
||||
final TextClassificationSessionId sessionId = options.getSessionId();
|
||||
final TextSelection.Request request = options.getRequest() != null
|
||||
? options.getRequest()
|
||||
: new TextSelection.Request.Builder(
|
||||
text, selectionStartIndex, selectionEndIndex)
|
||||
.setDefaultLocales(options.getDefaultLocales())
|
||||
.build();
|
||||
onSuggestSelection(sessionId, request, cancellationSignal, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Classifies the specified text and returns a {@link TextClassification} object that can be
|
||||
* used to generate a widget for handling the classified text.
|
||||
@@ -249,6 +272,26 @@ public abstract class TextClassifierService extends Service {
|
||||
@NonNull CancellationSignal cancellationSignal,
|
||||
@NonNull Callback<TextClassification> callback);
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/** @hide */
|
||||
public void onClassifyText(
|
||||
@NonNull CharSequence text,
|
||||
@IntRange(from = 0) int startIndex,
|
||||
@IntRange(from = 0) int endIndex,
|
||||
@Nullable TextClassification.Options options,
|
||||
@NonNull CancellationSignal cancellationSignal,
|
||||
@NonNull Callback<TextClassification> callback) {
|
||||
final TextClassificationSessionId sessionId = options.getSessionId();
|
||||
final TextClassification.Request request = options.getRequest() != null
|
||||
? options.getRequest()
|
||||
: new TextClassification.Request.Builder(
|
||||
text, startIndex, endIndex)
|
||||
.setDefaultLocales(options.getDefaultLocales())
|
||||
.setReferenceTime(options.getReferenceTime())
|
||||
.build();
|
||||
onClassifyText(sessionId, request, cancellationSignal, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates and returns a {@link TextLinks} that may be applied to the text to annotate it with
|
||||
* links information.
|
||||
@@ -264,6 +307,23 @@ public abstract class TextClassifierService extends Service {
|
||||
@NonNull CancellationSignal cancellationSignal,
|
||||
@NonNull Callback<TextLinks> callback);
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/** @hide */
|
||||
public void onGenerateLinks(
|
||||
@NonNull CharSequence text,
|
||||
@Nullable TextLinks.Options options,
|
||||
@NonNull CancellationSignal cancellationSignal,
|
||||
@NonNull Callback<TextLinks> callback) {
|
||||
final TextClassificationSessionId sessionId = options.getSessionId();
|
||||
final TextLinks.Request request = options.getRequest() != null
|
||||
? options.getRequest()
|
||||
: new TextLinks.Request.Builder(text)
|
||||
.setDefaultLocales(options.getDefaultLocales())
|
||||
.setEntityConfig(options.getEntityConfig())
|
||||
.build();
|
||||
onGenerateLinks(sessionId, request, cancellationSignal, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the selection event.
|
||||
* This is called when a selection event occurs. e.g. user changed selection; or smart selection
|
||||
|
||||
@@ -721,4 +721,67 @@ public final class TextClassification implements Parcelable {
|
||||
mEntityConfidence = EntityConfidence.CREATOR.createFromParcel(in);
|
||||
mId = in.readString();
|
||||
}
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/**
|
||||
* Optional input parameters for generating TextClassification.
|
||||
* @hide
|
||||
*/
|
||||
public static final class Options {
|
||||
|
||||
@Nullable private final TextClassificationSessionId mSessionId;
|
||||
@Nullable private final Request mRequest;
|
||||
@Nullable private LocaleList mDefaultLocales;
|
||||
@Nullable private ZonedDateTime mReferenceTime;
|
||||
|
||||
public Options() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
private Options(
|
||||
@Nullable TextClassificationSessionId sessionId, @Nullable Request request) {
|
||||
mSessionId = sessionId;
|
||||
mRequest = request;
|
||||
}
|
||||
|
||||
/** Helper to create Options from a Request. */
|
||||
public static Options from(TextClassificationSessionId sessionId, Request request) {
|
||||
final Options options = new Options(sessionId, request);
|
||||
options.setDefaultLocales(request.getDefaultLocales());
|
||||
options.setReferenceTime(request.getReferenceTime());
|
||||
return options;
|
||||
}
|
||||
|
||||
/** @param defaultLocales ordered list of locale preferences. */
|
||||
public Options setDefaultLocales(@Nullable LocaleList defaultLocales) {
|
||||
mDefaultLocales = defaultLocales;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @param referenceTime refrence time used for interpreting relatives dates */
|
||||
public Options setReferenceTime(@Nullable ZonedDateTime referenceTime) {
|
||||
mReferenceTime = referenceTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public LocaleList getDefaultLocales() {
|
||||
return mDefaultLocales;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ZonedDateTime getReferenceTime() {
|
||||
return mReferenceTime;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Request getRequest() {
|
||||
return mRequest;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public TextClassificationSessionId getSessionId() {
|
||||
return mSessionId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,6 +208,22 @@ public interface TextClassifier {
|
||||
return suggestSelection(request);
|
||||
}
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/** @hide */
|
||||
default TextSelection suggestSelection(
|
||||
@NonNull CharSequence text,
|
||||
@IntRange(from = 0) int selectionStartIndex,
|
||||
@IntRange(from = 0) int selectionEndIndex,
|
||||
@Nullable TextSelection.Options options) {
|
||||
final TextSelection.Request request = options.getRequest() != null
|
||||
? options.getRequest()
|
||||
: new TextSelection.Request.Builder(
|
||||
text, selectionStartIndex, selectionEndIndex)
|
||||
.setDefaultLocales(options.getDefaultLocales())
|
||||
.build();
|
||||
return suggestSelection(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Classifies the specified text and returns a {@link TextClassification} object that can be
|
||||
* used to generate a widget for handling the classified text.
|
||||
@@ -267,6 +283,23 @@ public interface TextClassifier {
|
||||
return classifyText(request);
|
||||
}
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/** @hide */
|
||||
default TextClassification classifyText(
|
||||
@NonNull CharSequence text,
|
||||
@IntRange(from = 0) int startIndex,
|
||||
@IntRange(from = 0) int endIndex,
|
||||
@Nullable TextClassification.Options options) {
|
||||
final TextClassification.Request request = options.getRequest() != null
|
||||
? options.getRequest()
|
||||
: new TextClassification.Request.Builder(
|
||||
text, startIndex, endIndex)
|
||||
.setDefaultLocales(options.getDefaultLocales())
|
||||
.setReferenceTime(options.getReferenceTime())
|
||||
.build();
|
||||
return classifyText(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates and returns a {@link TextLinks} that may be applied to the text to annotate it with
|
||||
* links information.
|
||||
@@ -288,6 +321,19 @@ public interface TextClassifier {
|
||||
return new TextLinks.Builder(request.getText().toString()).build();
|
||||
}
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/** @hide */
|
||||
default TextLinks generateLinks(
|
||||
@NonNull CharSequence text, @Nullable TextLinks.Options options) {
|
||||
final TextLinks.Request request = options.getRequest() != null
|
||||
? options.getRequest()
|
||||
: new TextLinks.Request.Builder(text)
|
||||
.setDefaultLocales(options.getDefaultLocales())
|
||||
.setEntityConfig(options.getEntityConfig())
|
||||
.build();
|
||||
return generateLinks(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximal length of text that can be processed by generateLinks.
|
||||
*
|
||||
@@ -377,6 +423,12 @@ public interface TextClassifier {
|
||||
/* includedEntityTypes */null, /* excludedEntityTypes */ null);
|
||||
}
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/** @hide */
|
||||
public static EntityConfig create(@Nullable Collection<String> hints) {
|
||||
return createWithHints(hints);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an EntityConfig.
|
||||
*
|
||||
@@ -406,6 +458,12 @@ public interface TextClassifier {
|
||||
/* includedEntityTypes */ entityTypes, /* excludedEntityTypes */ null);
|
||||
}
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/** @hide */
|
||||
public static EntityConfig createWithEntityList(@Nullable Collection<String> entityTypes) {
|
||||
return createWithExplicitEntityList(entityTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the final set of entities to find.
|
||||
*
|
||||
|
||||
@@ -28,6 +28,8 @@ import android.text.Spannable;
|
||||
import android.text.method.MovementMethod;
|
||||
import android.text.style.ClickableSpan;
|
||||
import android.text.style.URLSpan;
|
||||
import android.text.util.Linkify;
|
||||
import android.text.util.Linkify.LinkifyMask;
|
||||
import android.view.View;
|
||||
import android.view.textclassifier.TextClassifier.EntityType;
|
||||
import android.widget.TextView;
|
||||
@@ -607,4 +609,124 @@ public final class TextLinks implements Parcelable {
|
||||
return new TextLinks(mFullText, mLinks);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/**
|
||||
* Optional input parameters for generating TextLinks.
|
||||
* @hide
|
||||
*/
|
||||
public static final class Options {
|
||||
|
||||
@Nullable private final TextClassificationSessionId mSessionId;
|
||||
@Nullable private final Request mRequest;
|
||||
@Nullable private LocaleList mDefaultLocales;
|
||||
@Nullable private TextClassifier.EntityConfig mEntityConfig;
|
||||
private boolean mLegacyFallback;
|
||||
|
||||
private @ApplyStrategy int mApplyStrategy;
|
||||
private Function<TextLink, TextLinkSpan> mSpanFactory;
|
||||
|
||||
private String mCallingPackageName;
|
||||
|
||||
public Options() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
private Options(
|
||||
@Nullable TextClassificationSessionId sessionId, @Nullable Request request) {
|
||||
mSessionId = sessionId;
|
||||
mRequest = request;
|
||||
}
|
||||
|
||||
/** Helper to create Options from a Request. */
|
||||
public static Options from(TextClassificationSessionId sessionId, Request request) {
|
||||
final Options options = new Options(sessionId, request);
|
||||
options.setDefaultLocales(request.getDefaultLocales());
|
||||
options.setEntityConfig(request.getEntityConfig());
|
||||
return options;
|
||||
}
|
||||
|
||||
/** Returns a new options object based on the specified link mask. */
|
||||
public static Options fromLinkMask(@LinkifyMask int mask) {
|
||||
final List<String> entitiesToFind = new ArrayList<>();
|
||||
|
||||
if ((mask & Linkify.WEB_URLS) != 0) {
|
||||
entitiesToFind.add(TextClassifier.TYPE_URL);
|
||||
}
|
||||
if ((mask & Linkify.EMAIL_ADDRESSES) != 0) {
|
||||
entitiesToFind.add(TextClassifier.TYPE_EMAIL);
|
||||
}
|
||||
if ((mask & Linkify.PHONE_NUMBERS) != 0) {
|
||||
entitiesToFind.add(TextClassifier.TYPE_PHONE);
|
||||
}
|
||||
if ((mask & Linkify.MAP_ADDRESSES) != 0) {
|
||||
entitiesToFind.add(TextClassifier.TYPE_ADDRESS);
|
||||
}
|
||||
|
||||
return new Options().setEntityConfig(
|
||||
TextClassifier.EntityConfig.createWithEntityList(entitiesToFind));
|
||||
}
|
||||
|
||||
/** @param defaultLocales ordered list of locale preferences. */
|
||||
public Options setDefaultLocales(@Nullable LocaleList defaultLocales) {
|
||||
mDefaultLocales = defaultLocales;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @param entityConfig definition of which entity types to look for. */
|
||||
public Options setEntityConfig(@Nullable TextClassifier.EntityConfig entityConfig) {
|
||||
mEntityConfig = entityConfig;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @param applyStrategy strategy to use when resolving conflicts. */
|
||||
public Options setApplyStrategy(@ApplyStrategy int applyStrategy) {
|
||||
checkValidApplyStrategy(applyStrategy);
|
||||
mApplyStrategy = applyStrategy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @param spanFactory factory for converting TextLink to TextLinkSpan. */
|
||||
public Options setSpanFactory(@Nullable Function<TextLink, TextLinkSpan> spanFactory) {
|
||||
mSpanFactory = spanFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public LocaleList getDefaultLocales() {
|
||||
return mDefaultLocales;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public TextClassifier.EntityConfig getEntityConfig() {
|
||||
return mEntityConfig;
|
||||
}
|
||||
|
||||
@ApplyStrategy
|
||||
public int getApplyStrategy() {
|
||||
return mApplyStrategy;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Function<TextLink, TextLinkSpan> getSpanFactory() {
|
||||
return mSpanFactory;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Request getRequest() {
|
||||
return mRequest;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public TextClassificationSessionId getSessionId() {
|
||||
return mSessionId;
|
||||
}
|
||||
|
||||
private static void checkValidApplyStrategy(int applyStrategy) {
|
||||
if (applyStrategy != APPLY_STRATEGY_IGNORE && applyStrategy != APPLY_STRATEGY_REPLACE) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid apply strategy. See TextLinks.ApplyStrategy for options.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -375,4 +375,56 @@ public final class TextSelection implements Parcelable {
|
||||
mEntityConfidence = EntityConfidence.CREATOR.createFromParcel(in);
|
||||
mId = in.readString();
|
||||
}
|
||||
|
||||
|
||||
// TODO: Remove once apps can build against the latest sdk.
|
||||
/**
|
||||
* Optional input parameters for generating TextSelection.
|
||||
* @hide
|
||||
*/
|
||||
public static final class Options {
|
||||
|
||||
@Nullable private final TextClassificationSessionId mSessionId;
|
||||
@Nullable private final Request mRequest;
|
||||
@Nullable private LocaleList mDefaultLocales;
|
||||
private boolean mDarkLaunchAllowed;
|
||||
|
||||
public Options() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
private Options(
|
||||
@Nullable TextClassificationSessionId sessionId, @Nullable Request request) {
|
||||
mSessionId = sessionId;
|
||||
mRequest = request;
|
||||
}
|
||||
|
||||
/** Helper to create Options from a Request. */
|
||||
public static Options from(TextClassificationSessionId sessionId, Request request) {
|
||||
final Options options = new Options(sessionId, request);
|
||||
options.setDefaultLocales(request.getDefaultLocales());
|
||||
return options;
|
||||
}
|
||||
|
||||
/** @param defaultLocales ordered list of locale preferences. */
|
||||
public Options setDefaultLocales(@Nullable LocaleList defaultLocales) {
|
||||
mDefaultLocales = defaultLocales;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public LocaleList getDefaultLocales() {
|
||||
return mDefaultLocales;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Request getRequest() {
|
||||
return mRequest;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public TextClassificationSessionId getSessionId() {
|
||||
return mSessionId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user