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

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

Merge "Adding logging for generateLinks"

parents 72b7966b 31efdc38
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10479,6 +10479,7 @@ public final class Settings {
         * suggest_selection_max_range_length       (int)
         * classify_text_max_range_length           (int)
         * generate_links_max_text_length           (int)
         * generate_links_log_sample_rate           (int)
         * </pre>
         *
         * <p>
+7 −0
Original line number Diff line number Diff line
@@ -41,11 +41,13 @@ final class SystemTextClassifier implements TextClassifier {

    private final ITextClassifierService mManagerService;
    private final TextClassifier mFallback;
    private final String mPackageName;

    SystemTextClassifier(Context context) throws ServiceManager.ServiceNotFoundException {
        mManagerService = ITextClassifierService.Stub.asInterface(
                ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE));
        mFallback = new TextClassifierImpl(context);
        mPackageName = context.getPackageName();
    }

    /**
@@ -107,6 +109,11 @@ final class SystemTextClassifier implements TextClassifier {
            @NonNull CharSequence text, @Nullable TextLinks.Options options) {
        Utils.validate(text, false /* allowInMainThread */);
        try {
            if (options == null) {
                options = new TextLinks.Options().setCallingPackageName(mPackageName);
            } else if (!mPackageName.equals(options.getCallingPackageName())) {
                options.setCallingPackageName(mPackageName);
            }
            final TextLinksCallback callback = new TextLinksCallback();
            mManagerService.onGenerateLinks(text, options, callback);
            final TextLinks links = callback.mReceiver.get();
+12 −0
Original line number Diff line number Diff line
@@ -53,6 +53,8 @@ public final class TextClassifierConstants {
            "classify_text_max_range_length";
    private static final String GENERATE_LINKS_MAX_TEXT_LENGTH =
            "generate_links_max_text_length";
    private static final String GENERATE_LINKS_LOG_SAMPLE_RATE =
            "generate_links_log_sample_rate";

    private static final boolean SMART_SELECTION_DARK_LAUNCH_DEFAULT = false;
    private static final boolean SMART_SELECTION_ENABLED_FOR_EDIT_TEXT_DEFAULT = true;
@@ -60,6 +62,7 @@ public final class TextClassifierConstants {
    private static final int SUGGEST_SELECTION_MAX_RANGE_LENGTH_DEFAULT = 10 * 1000;
    private static final int CLASSIFY_TEXT_MAX_RANGE_LENGTH_DEFAULT = 10 * 1000;
    private static final int GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT = 100 * 1000;
    private static final int GENERATE_LINKS_LOG_SAMPLE_RATE_DEFAULT = 100;

    /** Default settings. */
    static final TextClassifierConstants DEFAULT = new TextClassifierConstants();
@@ -70,6 +73,7 @@ public final class TextClassifierConstants {
    private final int mSuggestSelectionMaxRangeLength;
    private final int mClassifyTextMaxRangeLength;
    private final int mGenerateLinksMaxTextLength;
    private final int mGenerateLinksLogSampleRate;

    private TextClassifierConstants() {
        mDarkLaunch = SMART_SELECTION_DARK_LAUNCH_DEFAULT;
@@ -78,6 +82,7 @@ public final class TextClassifierConstants {
        mSuggestSelectionMaxRangeLength = SUGGEST_SELECTION_MAX_RANGE_LENGTH_DEFAULT;
        mClassifyTextMaxRangeLength = CLASSIFY_TEXT_MAX_RANGE_LENGTH_DEFAULT;
        mGenerateLinksMaxTextLength = GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT;
        mGenerateLinksLogSampleRate = GENERATE_LINKS_LOG_SAMPLE_RATE_DEFAULT;
    }

    private TextClassifierConstants(@Nullable String settings) {
@@ -106,6 +111,9 @@ public final class TextClassifierConstants {
        mGenerateLinksMaxTextLength = parser.getInt(
                GENERATE_LINKS_MAX_TEXT_LENGTH,
                GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT);
        mGenerateLinksLogSampleRate = parser.getInt(
                GENERATE_LINKS_LOG_SAMPLE_RATE,
                GENERATE_LINKS_LOG_SAMPLE_RATE_DEFAULT);
    }

    static TextClassifierConstants loadFromString(String settings) {
@@ -135,4 +143,8 @@ public final class TextClassifierConstants {
    public int getGenerateLinksMaxTextLength() {
        return mGenerateLinksMaxTextLength;
    }

    public int getGenerateLinksLogSampleRate() {
        return mGenerateLinksLogSampleRate;
    }
}
+14 −3
Original line number Diff line number Diff line
@@ -36,10 +36,10 @@ import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.view.textclassifier.logging.DefaultLogger;
import android.view.textclassifier.logging.GenerateLinksLogger;
import android.view.textclassifier.logging.Logger;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.util.Preconditions;

import java.io.File;
@@ -92,7 +92,7 @@ public final class TextClassifierImpl implements TextClassifier {
    private final Context mContext;
    private final TextClassifier mFallback;

    private final MetricsLogger mMetricsLogger = new MetricsLogger();
    private final GenerateLinksLogger mGenerateLinksLogger;

    private final Object mLock = new Object();
    @GuardedBy("mLock") // Do not access outside this lock.
@@ -113,6 +113,8 @@ public final class TextClassifierImpl implements TextClassifier {
    public TextClassifierImpl(Context context) {
        mContext = Preconditions.checkNotNull(context);
        mFallback = TextClassifier.NO_OP;
        mGenerateLinksLogger = new GenerateLinksLogger(
                getSettings().getGenerateLinksLogSampleRate());
    }

    /** @inheritDoc */
@@ -226,6 +228,7 @@ public final class TextClassifierImpl implements TextClassifier {
        }

        try {
            final long startTimeMs = System.currentTimeMillis();
            final LocaleList defaultLocales = options != null ? options.getDefaultLocales() : null;
            final Calendar refTime = Calendar.getInstance();
            final Collection<String> entitiesToIdentify =
@@ -255,7 +258,15 @@ public final class TextClassifierImpl implements TextClassifier {
                }
                builder.addLink(span.getStartIndex(), span.getEndIndex(), entityScores);
            }
            return builder.build();
            final TextLinks links = builder.build();
            final long endTimeMs = System.currentTimeMillis();
            final String callingPackageName =
                    options == null || options.getCallingPackageName() == null
                            ? mContext.getPackageName()  // local (in process) TC.
                            : options.getCallingPackageName();
            mGenerateLinksLogger.logGenerateLinks(
                    text, links, callingPackageName, endTimeMs - startTimeMs);
            return links;
        } catch (Throwable t) {
            // Avoid throwing from this method. Log the error.
            Log.e(LOG_TAG, "Error getting links info.", t);
+23 −0
Original line number Diff line number Diff line
@@ -305,6 +305,8 @@ public final class TextLinks implements Parcelable {
        private @ApplyStrategy int mApplyStrategy;
        private Function<TextLink, TextLinkSpan> mSpanFactory;

        private String mCallingPackageName;

        /**
         * Returns a new options object based on the specified link mask.
         */
@@ -376,6 +378,15 @@ public final class TextLinks implements Parcelable {
            return this;
        }

        /**
         * Sets the name of the package that requested the links to get generated.
         * @hide
         */
        public Options setCallingPackageName(@Nullable String callingPackageName) {
            mCallingPackageName = callingPackageName;
            return this;
        }

        /**
         * @return ordered list of locale preferences that can be used to disambiguate
         *      the provided text
@@ -417,6 +428,16 @@ public final class TextLinks implements Parcelable {
            return mSpanFactory;
        }

        /**
         * @return the name of the package that requested the links to get generated.
         * TODO: make available as system API
         * @hide
         */
        @Nullable
        public String getCallingPackageName() {
            return mCallingPackageName;
        }

        @Override
        public int describeContents() {
            return 0;
@@ -433,6 +454,7 @@ public final class TextLinks implements Parcelable {
                mEntityConfig.writeToParcel(dest, flags);
            }
            dest.writeInt(mApplyStrategy);
            dest.writeString(mCallingPackageName);
        }

        public static final Parcelable.Creator<Options> CREATOR =
@@ -456,6 +478,7 @@ public final class TextLinks implements Parcelable {
                mEntityConfig = TextClassifier.EntityConfig.CREATOR.createFromParcel(in);
            }
            mApplyStrategy = in.readInt();
            mCallingPackageName = in.readString();
        }
    }

Loading