diff options
| author | 2018-11-08 20:45:20 +0000 | |
|---|---|---|
| committer | 2018-11-13 12:09:22 +0000 | |
| commit | 916bb9e37e2eab6661e41d52cf7ea81c4584c758 (patch) | |
| tree | d43c75771c80ebe41f5eba252d3998989f58fc70 | |
| parent | 50619770805796dbec93eb9378c8dc21f6f66f7d (diff) | |
Add get/setExtras to TextLinks and TextSelection
Test: atest frameworks/base/core/tests/coretests/src/android/view/textclassifier/TextSelectionTest.java
Test: atest frameworks/base/core/tests/coretests/src/android/view/textclassifier/TextLinksTest.java
Test: atest cts/tests/tests/view/src/android/view/textclassifier/cts/TextClassifierValueObjectsTest.java
BUG: 118690735
Change-Id: I5786db40b24b38020514f96b1d642835c9a9db51
5 files changed, 156 insertions, 9 deletions
diff --git a/api/current.txt b/api/current.txt index 70d7fd29d55e..7e03cb6b26a1 100755 --- a/api/current.txt +++ b/api/current.txt @@ -51792,6 +51792,7 @@ package android.view.textclassifier { public final class TextLinks implements android.os.Parcelable { method public int apply(android.text.Spannable, int, java.util.function.Function<android.view.textclassifier.TextLinks.TextLink, android.view.textclassifier.TextLinks.TextLinkSpan>); method public int describeContents(); + method public android.os.Bundle getExtras(); method public java.util.Collection<android.view.textclassifier.TextLinks.TextLink> getLinks(); method public void writeToParcel(android.os.Parcel, int); field public static final int APPLY_STRATEGY_IGNORE = 0; // 0x0 @@ -51808,12 +51809,14 @@ package android.view.textclassifier { method public android.view.textclassifier.TextLinks.Builder addLink(int, int, java.util.Map<java.lang.String, java.lang.Float>); method public android.view.textclassifier.TextLinks build(); method public android.view.textclassifier.TextLinks.Builder clearTextLinks(); + method public android.view.textclassifier.TextLinks.Builder setExtras(android.os.Bundle); } public static final class TextLinks.Request implements android.os.Parcelable { method public int describeContents(); method public android.os.LocaleList getDefaultLocales(); method public android.view.textclassifier.TextClassifier.EntityConfig getEntityConfig(); + method public android.os.Bundle getExtras(); method public java.lang.CharSequence getText(); method public void writeToParcel(android.os.Parcel, int); field public static final android.os.Parcelable.Creator<android.view.textclassifier.TextLinks.Request> CREATOR; @@ -51824,6 +51827,7 @@ package android.view.textclassifier { method public android.view.textclassifier.TextLinks.Request build(); method public android.view.textclassifier.TextLinks.Request.Builder setDefaultLocales(android.os.LocaleList); method public android.view.textclassifier.TextLinks.Request.Builder setEntityConfig(android.view.textclassifier.TextClassifier.EntityConfig); + method public android.view.textclassifier.TextLinks.Request.Builder setExtras(android.os.Bundle); } public static final class TextLinks.TextLink implements android.os.Parcelable { @@ -51848,6 +51852,7 @@ package android.view.textclassifier { method public float getConfidenceScore(java.lang.String); method public java.lang.String getEntity(int); method public int getEntityCount(); + method public android.os.Bundle getExtras(); method public java.lang.String getId(); method public int getSelectionEndIndex(); method public int getSelectionStartIndex(); @@ -51859,6 +51864,7 @@ package android.view.textclassifier { ctor public TextSelection.Builder(int, int); method public android.view.textclassifier.TextSelection build(); method public android.view.textclassifier.TextSelection.Builder setEntityType(java.lang.String, float); + method public android.view.textclassifier.TextSelection.Builder setExtras(android.os.Bundle); method public android.view.textclassifier.TextSelection.Builder setId(java.lang.String); } @@ -51866,6 +51872,7 @@ package android.view.textclassifier { method public int describeContents(); method public android.os.LocaleList getDefaultLocales(); method public int getEndIndex(); + method public android.os.Bundle getExtras(); method public int getStartIndex(); method public java.lang.CharSequence getText(); method public void writeToParcel(android.os.Parcel, int); @@ -51876,6 +51883,7 @@ package android.view.textclassifier { ctor public TextSelection.Request.Builder(java.lang.CharSequence, int, int); method public android.view.textclassifier.TextSelection.Request build(); method public android.view.textclassifier.TextSelection.Request.Builder setDefaultLocales(android.os.LocaleList); + method public android.view.textclassifier.TextSelection.Request.Builder setExtras(android.os.Bundle); } } diff --git a/core/java/android/view/textclassifier/TextLinks.java b/core/java/android/view/textclassifier/TextLinks.java index b31438f757e2..02aee508787e 100644 --- a/core/java/android/view/textclassifier/TextLinks.java +++ b/core/java/android/view/textclassifier/TextLinks.java @@ -21,6 +21,7 @@ import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; import android.content.Context; +import android.os.Bundle; import android.os.LocaleList; import android.os.Parcel; import android.os.Parcelable; @@ -92,10 +93,12 @@ public final class TextLinks implements Parcelable { private final String mFullText; private final List<TextLink> mLinks; + private final Bundle mExtras; - private TextLinks(String fullText, ArrayList<TextLink> links) { + private TextLinks(String fullText, ArrayList<TextLink> links, Bundle extras) { mFullText = fullText; mLinks = Collections.unmodifiableList(links); + mExtras = extras; } /** @@ -116,6 +119,18 @@ public final class TextLinks implements Parcelable { } /** + * Returns the extended data. + * + * <p><b>NOTE: </b>Each call to this method returns a new bundle copy so clients should + * prefer to hold a reference to the returned bundle rather than frequently calling this + * method. + */ + @NonNull + public Bundle getExtras() { + return mExtras.deepCopy(); + } + + /** * Annotates the given text with the generated links. It will fail if the provided text doesn't * match the original text used to create the TextLinks. * @@ -158,6 +173,7 @@ public final class TextLinks implements Parcelable { public void writeToParcel(Parcel dest, int flags) { dest.writeString(mFullText); dest.writeTypedList(mLinks); + dest.writeBundle(mExtras); } public static final Parcelable.Creator<TextLinks> CREATOR = @@ -176,6 +192,7 @@ public final class TextLinks implements Parcelable { private TextLinks(Parcel in) { mFullText = in.readString(); mLinks = in.createTypedArrayList(TextLink.CREATOR); + mExtras = in.readBundle(); } /** @@ -304,18 +321,21 @@ public final class TextLinks implements Parcelable { @Nullable private final TextClassifier.EntityConfig mEntityConfig; private final boolean mLegacyFallback; private String mCallingPackageName; + private final Bundle mExtras; private Request( CharSequence text, LocaleList defaultLocales, TextClassifier.EntityConfig entityConfig, boolean legacyFallback, - String callingPackageName) { + String callingPackageName, + Bundle extras) { mText = text; mDefaultLocales = defaultLocales; mEntityConfig = entityConfig; mLegacyFallback = legacyFallback; mCallingPackageName = callingPackageName; + mExtras = extras; } /** @@ -362,6 +382,18 @@ public final class TextLinks implements Parcelable { } /** + * Returns the extended data. + * + * <p><b>NOTE: </b>Each call to this method returns a new bundle copy so clients should + * prefer to hold a reference to the returned bundle rather than frequently calling this + * method. + */ + @NonNull + public Bundle getExtras() { + return mExtras.deepCopy(); + } + + /** * A builder for building TextLinks requests. */ public static final class Builder { @@ -372,6 +404,7 @@ public final class TextLinks implements Parcelable { @Nullable private TextClassifier.EntityConfig mEntityConfig; private boolean mLegacyFallback = true; // Use legacy fall back by default. private String mCallingPackageName; + @Nullable private Bundle mExtras; public Builder(@NonNull CharSequence text) { mText = Preconditions.checkNotNull(text); @@ -431,15 +464,25 @@ public final class TextLinks implements Parcelable { } /** + * Sets the extended data. + * + * @return this builder + */ + public Builder setExtras(@Nullable Bundle extras) { + mExtras = extras; + return this; + } + + /** * Builds and returns the request object. */ @NonNull public Request build() { return new Request( mText, mDefaultLocales, mEntityConfig, - mLegacyFallback, mCallingPackageName); + mLegacyFallback, mCallingPackageName, + mExtras == null ? Bundle.EMPTY : mExtras.deepCopy()); } - } /** @@ -469,6 +512,7 @@ public final class TextLinks implements Parcelable { mEntityConfig.writeToParcel(dest, flags); } dest.writeString(mCallingPackageName); + dest.writeBundle(mExtras); } public static final Parcelable.Creator<Request> CREATOR = @@ -491,6 +535,7 @@ public final class TextLinks implements Parcelable { ? null : TextClassifier.EntityConfig.CREATOR.createFromParcel(in); mLegacyFallback = true; mCallingPackageName = in.readString(); + mExtras = in.readBundle(); } } @@ -575,6 +620,7 @@ public final class TextLinks implements Parcelable { public static final class Builder { private final String mFullText; private final ArrayList<TextLink> mLinks; + private Bundle mExtras; /** * Create a new TextLinks.Builder. @@ -622,13 +668,24 @@ public final class TextLinks implements Parcelable { } /** + * Sets the extended data. + * + * @return this builder + */ + public Builder setExtras(@Nullable Bundle extras) { + mExtras = extras; + return this; + } + + /** * Constructs a TextLinks instance. * * @return the constructed TextLinks */ @NonNull public TextLinks build() { - return new TextLinks(mFullText, mLinks); + return new TextLinks(mFullText, mLinks, + mExtras == null ? Bundle.EMPTY : mExtras.deepCopy()); } } } diff --git a/core/java/android/view/textclassifier/TextSelection.java b/core/java/android/view/textclassifier/TextSelection.java index 52d01ea6b035..f23691586a2a 100644 --- a/core/java/android/view/textclassifier/TextSelection.java +++ b/core/java/android/view/textclassifier/TextSelection.java @@ -20,6 +20,7 @@ import android.annotation.FloatRange; import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.Nullable; +import android.os.Bundle; import android.os.LocaleList; import android.os.Parcel; import android.os.Parcelable; @@ -41,13 +42,16 @@ public final class TextSelection implements Parcelable { private final int mEndIndex; private final EntityConfidence mEntityConfidence; @Nullable private final String mId; + private final Bundle mExtras; private TextSelection( - int startIndex, int endIndex, Map<String, Float> entityConfidence, String id) { + int startIndex, int endIndex, Map<String, Float> entityConfidence, String id, + Bundle extras) { mStartIndex = startIndex; mEndIndex = endIndex; mEntityConfidence = new EntityConfidence(entityConfidence); mId = id; + mExtras = extras; } /** @@ -103,6 +107,18 @@ public final class TextSelection implements Parcelable { return mId; } + /** + * Returns the extended data. + * + * <p><b>NOTE: </b>Each call to this method returns a new bundle copy so clients should + * prefer to hold a reference to the returned bundle rather than frequently calling this + * method. + */ + @NonNull + public Bundle getExtras() { + return mExtras.deepCopy(); + } + @Override public String toString() { return String.format( @@ -120,6 +136,8 @@ public final class TextSelection implements Parcelable { private final int mEndIndex; private final Map<String, Float> mEntityConfidence = new ArrayMap<>(); @Nullable private String mId; + @Nullable + private Bundle mExtras; /** * Creates a builder used to build {@link TextSelection} objects. @@ -160,12 +178,23 @@ public final class TextSelection implements Parcelable { } /** + * Sets the extended data. + * + * @return this builder + */ + public Builder setExtras(@Nullable Bundle extras) { + mExtras = extras; + return this; + } + + /** * Builds and returns {@link TextSelection} object. */ @NonNull public TextSelection build() { return new TextSelection( - mStartIndex, mEndIndex, mEntityConfidence, mId); + mStartIndex, mEndIndex, mEntityConfidence, mId, + mExtras == null ? Bundle.EMPTY : mExtras.deepCopy()); } } @@ -179,18 +208,21 @@ public final class TextSelection implements Parcelable { private final int mEndIndex; @Nullable private final LocaleList mDefaultLocales; private final boolean mDarkLaunchAllowed; + private final Bundle mExtras; private Request( CharSequence text, int startIndex, int endIndex, LocaleList defaultLocales, - boolean darkLaunchAllowed) { + boolean darkLaunchAllowed, + Bundle extras) { mText = text; mStartIndex = startIndex; mEndIndex = endIndex; mDefaultLocales = defaultLocales; mDarkLaunchAllowed = darkLaunchAllowed; + mExtras = extras; } /** @@ -238,6 +270,18 @@ public final class TextSelection implements Parcelable { } /** + * Returns the extended data. + * + * <p><b>NOTE: </b>Each call to this method returns a new bundle copy so clients should + * prefer to hold a reference to the returned bundle rather than frequently calling this + * method. + */ + @NonNull + public Bundle getExtras() { + return mExtras.deepCopy(); + } + + /** * A builder for building TextSelection requests. */ public static final class Builder { @@ -248,6 +292,7 @@ public final class TextSelection implements Parcelable { @Nullable private LocaleList mDefaultLocales; private boolean mDarkLaunchAllowed; + private Bundle mExtras; /** * @param text text providing context for the selected text (which is specified by the @@ -296,12 +341,23 @@ public final class TextSelection implements Parcelable { } /** + * Sets the extended data. + * + * @return this builder + */ + public Builder setExtras(@Nullable Bundle extras) { + mExtras = extras; + return this; + } + + /** * Builds and returns the request object. */ @NonNull public Request build() { return new Request(mText, mStartIndex, mEndIndex, - mDefaultLocales, mDarkLaunchAllowed); + mDefaultLocales, mDarkLaunchAllowed, + mExtras == null ? Bundle.EMPTY : mExtras.deepCopy()); } } @@ -319,6 +375,7 @@ public final class TextSelection implements Parcelable { if (mDefaultLocales != null) { mDefaultLocales.writeToParcel(dest, flags); } + dest.writeBundle(mExtras); } public static final Parcelable.Creator<Request> CREATOR = @@ -340,6 +397,7 @@ public final class TextSelection implements Parcelable { mEndIndex = in.readInt(); mDefaultLocales = in.readInt() == 0 ? null : LocaleList.CREATOR.createFromParcel(in); mDarkLaunchAllowed = false; + mExtras = in.readBundle(); } } @@ -354,6 +412,7 @@ public final class TextSelection implements Parcelable { dest.writeInt(mEndIndex); mEntityConfidence.writeToParcel(dest, flags); dest.writeString(mId); + dest.writeBundle(mExtras); } public static final Parcelable.Creator<TextSelection> CREATOR = @@ -374,5 +433,6 @@ public final class TextSelection implements Parcelable { mEndIndex = in.readInt(); mEntityConfidence = EntityConfidence.CREATOR.createFromParcel(in); mId = in.readString(); + mExtras = in.readBundle(); } } diff --git a/core/tests/coretests/src/android/view/textclassifier/TextLinksTest.java b/core/tests/coretests/src/android/view/textclassifier/TextLinksTest.java index fff723fc544c..f6ec0e6480f2 100644 --- a/core/tests/coretests/src/android/view/textclassifier/TextLinksTest.java +++ b/core/tests/coretests/src/android/view/textclassifier/TextLinksTest.java @@ -18,6 +18,7 @@ package android.view.textclassifier; import static org.junit.Assert.assertEquals; +import android.os.Bundle; import android.os.LocaleList; import android.os.Parcel; import android.support.test.filters.SmallTest; @@ -38,6 +39,12 @@ import java.util.Map; @SmallTest @RunWith(AndroidJUnit4.class) public class TextLinksTest { + private static final String BUNDLE_KEY = "key"; + private static final String BUNDLE_VALUE = "value"; + private static final Bundle BUNDLE = new Bundle(); + static { + BUNDLE.putString(BUNDLE_KEY, BUNDLE_VALUE); + } private Map<String, Float> getEntityScores(float address, float phone, float other) { final Map<String, Float> result = new ArrayMap<>(); @@ -59,6 +66,7 @@ public class TextLinksTest { final TextLinks reference = new TextLinks.Builder(fullText) .addLink(0, 4, getEntityScores(0.f, 0.f, 1.f)) .addLink(5, 12, getEntityScores(.8f, .1f, .5f)) + .setExtras(BUNDLE) .build(); // Parcel and unparcel. @@ -83,6 +91,7 @@ public class TextLinksTest { assertEquals(.8f, resultList.get(1).getConfidenceScore(TextClassifier.TYPE_ADDRESS), 1e-7f); assertEquals(.5f, resultList.get(1).getConfidenceScore(TextClassifier.TYPE_OTHER), 1e-7f); assertEquals(.1f, resultList.get(1).getConfidenceScore(TextClassifier.TYPE_PHONE), 1e-7f); + assertEquals(BUNDLE_VALUE, result.getExtras().getString(BUNDLE_KEY)); } @Test @@ -94,6 +103,7 @@ public class TextLinksTest { final TextLinks.Request reference = new TextLinks.Request.Builder("text") .setDefaultLocales(new LocaleList(Locale.US, Locale.GERMANY)) .setEntityConfig(entityConfig) + .setExtras(BUNDLE) .build(); // Parcel and unparcel. @@ -108,5 +118,6 @@ public class TextLinksTest { result.getEntityConfig().getHints().toArray()); assertEquals(new HashSet<String>(Arrays.asList("a", "c")), result.getEntityConfig().resolveEntityListModifications(Collections.emptyList())); + assertEquals(BUNDLE_VALUE, result.getExtras().getString(BUNDLE_KEY)); } } diff --git a/core/tests/coretests/src/android/view/textclassifier/TextSelectionTest.java b/core/tests/coretests/src/android/view/textclassifier/TextSelectionTest.java index 4855dada361a..7ea5108bc3bb 100644 --- a/core/tests/coretests/src/android/view/textclassifier/TextSelectionTest.java +++ b/core/tests/coretests/src/android/view/textclassifier/TextSelectionTest.java @@ -18,6 +18,7 @@ package android.view.textclassifier; import static org.junit.Assert.assertEquals; +import android.os.Bundle; import android.os.LocaleList; import android.os.Parcel; import android.support.test.filters.SmallTest; @@ -31,6 +32,12 @@ import java.util.Locale; @SmallTest @RunWith(AndroidJUnit4.class) public class TextSelectionTest { + private static final String BUNDLE_KEY = "key"; + private static final String BUNDLE_VALUE = "value"; + private static final Bundle BUNDLE = new Bundle(); + static { + BUNDLE.putString(BUNDLE_KEY, BUNDLE_VALUE); + } @Test public void testParcel() { @@ -42,6 +49,7 @@ public class TextSelectionTest { .setEntityType(TextClassifier.TYPE_PHONE, 0.7f) .setEntityType(TextClassifier.TYPE_URL, 0.1f) .setId(id) + .setExtras(BUNDLE) .build(); // Parcel and unparcel @@ -61,6 +69,7 @@ public class TextSelectionTest { assertEquals(0.7f, result.getConfidenceScore(TextClassifier.TYPE_PHONE), 1e-7f); assertEquals(0.3f, result.getConfidenceScore(TextClassifier.TYPE_ADDRESS), 1e-7f); assertEquals(0.1f, result.getConfidenceScore(TextClassifier.TYPE_URL), 1e-7f); + assertEquals(BUNDLE_VALUE, result.getExtras().getString(BUNDLE_KEY)); } @Test @@ -69,6 +78,7 @@ public class TextSelectionTest { final TextSelection.Request reference = new TextSelection.Request.Builder(text, 0, text.length()) .setDefaultLocales(new LocaleList(Locale.US, Locale.GERMANY)) + .setExtras(BUNDLE) .build(); // Parcel and unparcel. @@ -81,5 +91,6 @@ public class TextSelectionTest { assertEquals(0, result.getStartIndex()); assertEquals(text.length(), result.getEndIndex()); assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags()); + assertEquals(BUNDLE_VALUE, result.getExtras().getString(BUNDLE_KEY)); } } |