Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b34837e authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add get/setExtras to TextLinks and TextSelection"

parents 36154fdb 916bb9e3
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -52053,6 +52053,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
@@ -52069,12 +52070,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;
@@ -52085,6 +52088,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 {
@@ -52109,6 +52113,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();
@@ -52120,6 +52125,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);
  }
@@ -52127,6 +52133,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);
@@ -52137,6 +52144,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);
  }
}
+62 −5
Original line number Diff line number Diff line
@@ -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;
    }

    /**
@@ -115,6 +118,18 @@ public final class TextLinks implements Parcelable {
        return mLinks;
    }

    /**
     * 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;
        }

        /**
@@ -361,6 +381,18 @@ public final class TextLinks implements Parcelable {
            mCallingPackageName = callingPackageName;
        }

        /**
         * 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.
         */
@@ -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);
@@ -430,6 +463,16 @@ public final class TextLinks implements Parcelable {
                return this;
            }

            /**
             * Sets the extended data.
             *
             * @return this builder
             */
            public Builder setExtras(@Nullable Bundle extras) {
                mExtras = extras;
                return this;
            }

            /**
             * Builds and returns the request object.
             */
@@ -437,9 +480,9 @@ public final class TextLinks implements Parcelable {
            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.
@@ -621,6 +667,16 @@ public final class TextLinks implements Parcelable {
            return this;
        }

        /**
         * Sets the extended data.
         *
         * @return this builder
         */
        public Builder setExtras(@Nullable Bundle extras) {
            mExtras = extras;
            return this;
        }

        /**
         * Constructs a TextLinks instance.
         *
@@ -628,7 +684,8 @@ public final class TextLinks implements Parcelable {
         */
        @NonNull
        public TextLinks build() {
            return new TextLinks(mFullText, mLinks);
            return new TextLinks(mFullText, mLinks,
                    mExtras == null ? Bundle.EMPTY : mExtras.deepCopy());
        }
    }
}
+64 −4
Original line number Diff line number Diff line
@@ -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.
@@ -159,13 +177,24 @@ public final class TextSelection implements Parcelable {
            return this;
        }

        /**
         * 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;
        }

        /**
@@ -237,6 +269,18 @@ public final class TextSelection implements Parcelable {
            return mDefaultLocales;
        }

        /**
         * 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.
         */
@@ -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
@@ -295,13 +340,24 @@ public final class TextSelection implements Parcelable {
                return this;
            }

            /**
             * 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();
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -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));
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -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));
    }
}