类android.view.textclassifier.TextClassifier源码实例Demo

下面列出了怎么用android.view.textclassifier.TextClassifier的API类实例代码及写法,或者点击链接到github查看源代码。

/**
 * Creates a "selection modified" event.
 * Use when a TextClassifier modifies the selection.
 *
 * @param start  the start word (inclusive) index of the selection
 * @param end  the end word (exclusive) index of the selection
 * @param selection  the TextSelection object returned by the TextClassifier for the
 *      specified selection
 */
public static SelectionEvent selectionModified(
        int start, int end, @NonNull TextSelection selection) {
    final boolean smartSelection = getSourceClassifier(selection.getId())
            .equals(TextClassifier.DEFAULT_LOG_TAG);
    final int eventType;
    if (smartSelection) {
        eventType = end - start > 1
                ? EventType.SMART_SELECTION_MULTI
                : EventType.SMART_SELECTION_SINGLE;

    } else {
        eventType = EventType.AUTO_SELECTION;
    }
    final String entityType = selection.getEntityCount() > 0
            ? selection.getEntity(0)
            : TextClassifier.TYPE_UNKNOWN;
    final String versionTag = getVersionInfo(selection.getId());
    return new SelectionEvent(start, end, eventType, entityType, versionTag);
}
 
public void logSelectionStarted(
        TextClassifier classificationSession,
        CharSequence text, int index,
        @InvocationMethod int invocationMethod) {
    try {
        Preconditions.checkNotNull(text);
        Preconditions.checkArgumentInRange(index, 0, text.length(), "index");
        if (mText == null || !mText.contentEquals(text)) {
            mText = text.toString();
        }
        mTokenIterator.setText(mText);
        mStartIndex = index;
        mClassificationSession = classificationSession;
        if (hasActiveClassificationSession()) {
            mClassificationSession.onSelectionEvent(
                    SelectionEvent.createSelectionStartedEvent(invocationMethod, 0));
        }
    } catch (Exception e) {
        // Avoid crashes due to logging.
        Log.e(LOG_TAG, "" + e.getMessage(), e);
    }
}
 
private SelectionEvent(
        int start, int end, int eventType,
        @TextClassifier.EntityType String entityType, String versionTag) {
    Preconditions.checkArgument(end >= start, "end cannot be less than start");
    mStart = start;
    mEnd = end;
    mEventType = eventType;
    mEntityType = Preconditions.checkNotNull(entityType);
    mVersionTag = Preconditions.checkNotNull(versionTag);
}
 
/**
 * Creates a "selection modified" event.
 * Use when the user modifies the selection and the selection's entity type is known.
 *
 * @param start  the start word (inclusive) index of the selection
 * @param end  the end word (exclusive) index of the selection
 * @param classification  the TextClassification object returned by the TextClassifier that
 *      classified the selected text
 */
public static SelectionEvent selectionModified(
        int start, int end, @NonNull TextClassification classification) {
    final String entityType = classification.getEntityCount() > 0
            ? classification.getEntity(0)
            : TextClassifier.TYPE_UNKNOWN;
    final String versionTag = getVersionInfo(classification.getId());
    return new SelectionEvent(
            start, end, EventType.SELECTION_MODIFIED, entityType, versionTag);
}
 
源代码5 项目: android_9.0.0_r45   文件: TextClassifierService.java
/**
 * Returns a TextClassifier that runs in this service's process.
 * If the local TextClassifier is disabled, this returns {@link TextClassifier#NO_OP}.
 */
public final TextClassifier getLocalTextClassifier() {
    final TextClassificationManager tcm = getSystemService(TextClassificationManager.class);
    if (tcm != null) {
        return tcm.getTextClassifier(TextClassifier.LOCAL);
    }
    return TextClassifier.NO_OP;
}
 
@TextClassifier.WidgetType
private static String getWidetType(TextView textView) {
    if (textView.isTextEditable()) {
        return TextClassifier.WIDGET_TYPE_EDITTEXT;
    }
    if (textView.isTextSelectable()) {
        return TextClassifier.WIDGET_TYPE_TEXTVIEW;
    }
    return TextClassifier.WIDGET_TYPE_UNSELECTABLE_TEXTVIEW;
}
 
@UiThread
public void init(Supplier<TextClassifier> textClassifier, CharSequence text,
        int selectionStart, int selectionEnd, LocaleList locales) {
    mTextClassifier = Preconditions.checkNotNull(textClassifier);
    mText = Preconditions.checkNotNull(text).toString();
    mLastClassificationText = null; // invalidate.
    Preconditions.checkArgument(selectionEnd > selectionStart);
    mSelectionStart = selectionStart;
    mSelectionEnd = selectionEnd;
    mDefaultLocales = locales;
}
 
源代码8 项目: cronet   文件: ApiCompatibilityUtils.java
/**
 * Disables the Smart Select {@link TextClassifier} for the given {@link TextView} instance.
 * @param textView The {@link TextView} that should have its classifier disabled.
 */
@TargetApi(Build.VERSION_CODES.O)
public static void disableSmartSelectionTextClassifier(TextView textView) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;

    textView.setTextClassifier(TextClassifier.NO_OP);
}
 
源代码9 项目: android_9.0.0_r45   文件: WebView.java
/**
 * Sets the {@link TextClassifier} for this WebView.
 */
public void setTextClassifier(@Nullable TextClassifier textClassifier) {
    mProvider.setTextClassifier(textClassifier);
}
 
源代码10 项目: android_9.0.0_r45   文件: WebView.java
/**
 * Returns the {@link TextClassifier} used by this WebView.
 * If no TextClassifier has been set, this WebView uses the default set by the system.
 */
@NonNull
public TextClassifier getTextClassifier() {
    return mProvider.getTextClassifier();
}
 
源代码11 项目: android_9.0.0_r45   文件: WebViewProvider.java
@SuppressWarnings("unused")
public default void setTextClassifier(@Nullable TextClassifier textClassifier) {}
 
源代码12 项目: android_9.0.0_r45   文件: WebViewProvider.java
@NonNull
public default TextClassifier getTextClassifier() { return TextClassifier.NO_OP; }
 
/**
 * Creates a "selection started" event.
 *
 * @param start  the word index of the selected word
 */
public static SelectionEvent selectionStarted(int start) {
    return new SelectionEvent(
            start, start + 1, EventType.SELECTION_STARTED,
            TextClassifier.TYPE_UNKNOWN, NO_VERSION_TAG);
}
 
源代码14 项目: android_9.0.0_r45   文件: Linkify.java
private static Future<Void> addLinksAsync(
        @NonNull Spannable text,
        @NonNull TextClassifier classifier,
        @Nullable TextLinksParams params,
        @Nullable Executor executor,
        @Nullable Consumer<Integer> callback,
        @Nullable Runnable modifyTextView) {
    Preconditions.checkNotNull(text);
    Preconditions.checkNotNull(classifier);

    // TODO: This is a bug. We shouldnot call getMaxGenerateLinksTextLength() on the UI thread.
    // The input text may exceed the maximum length the text classifier can handle. In such
    // cases, we process the text up to the maximum length.
    final CharSequence truncatedText = text.subSequence(
            0, Math.min(text.length(), classifier.getMaxGenerateLinksTextLength()));

    final TextClassifier.EntityConfig entityConfig = (params == null)
            ? null : params.getEntityConfig();
    final TextLinks.Request request = new TextLinks.Request.Builder(truncatedText)
            .setLegacyFallback(true)
            .setEntityConfig(entityConfig)
            .build();
    final Supplier<TextLinks> supplier = () -> classifier.generateLinks(request);
    final Consumer<TextLinks> consumer = links -> {
        if (links.getLinks().isEmpty()) {
            if (callback != null) {
                callback.accept(TextLinks.STATUS_NO_LINKS_FOUND);
            }
            return;
        }

        // Remove spans only for the part of the text we generated links for.
        final TextLinkSpan[] old =
                text.getSpans(0, truncatedText.length(), TextLinkSpan.class);
        for (int i = old.length - 1; i >= 0; i--) {
            text.removeSpan(old[i]);
        }

        final @TextLinks.Status int result = params.apply(text, links);
        if (result == TextLinks.STATUS_LINKS_APPLIED) {
            if (modifyTextView != null) {
                modifyTextView.run();
            }
        }
        if (callback != null) {
            callback.accept(result);
        }
    };
    if (executor == null) {
        return CompletableFuture.supplyAsync(supplier).thenAccept(consumer);
    } else {
        return CompletableFuture.supplyAsync(supplier, executor).thenAccept(consumer);
    }
}
 
TextClassificationHelper(Context context, Supplier<TextClassifier> textClassifier,
        CharSequence text, int selectionStart, int selectionEnd, LocaleList locales) {
    init(textClassifier, text, selectionStart, selectionEnd, locales);
    mContext = Preconditions.checkNotNull(context);
}
 
源代码16 项目: timecat   文件: TEditText.java
@Override
public void setTextClassifier(@Nullable TextClassifier textClassifier) {
    super.setTextClassifier(textClassifier);
}
 
源代码17 项目: timecat   文件: TEditText.java
@NonNull
@Override
public TextClassifier getTextClassifier() {
    return super.getTextClassifier();
}
 
/**
 * Creates an event specifying an action taken on a selection.
 * Use when the user clicks on an action to act on the selected text and the selection's
 * entity type is known.
 *
 * @param start  the start word (inclusive) index of the selection
 * @param end  the end word (exclusive) index of the selection
 * @param actionType  the action that was performed on the selection
 * @param classification  the TextClassification object returned by the TextClassifier that
 *      classified the selected text
 */
public static SelectionEvent selectionAction(
        int start, int end, @ActionType int actionType,
        @NonNull TextClassification classification) {
    final String entityType = classification.getEntityCount() > 0
            ? classification.getEntity(0)
            : TextClassifier.TYPE_UNKNOWN;
    final String versionTag = getVersionInfo(classification.getId());
    return new SelectionEvent(start, end, actionType, entityType, versionTag);
}
 
源代码19 项目: android_9.0.0_r45   文件: Linkify.java
/**
 * Scans the text of the provided TextView and turns all occurrences of the entity types
 * specified by {@code options} into clickable links. If links are found, this method
 * removes any pre-existing {@link TextLinkSpan} attached to the text to avoid
 * problems if you call it repeatedly on the same text.
 *
 * <p><strong>Note:</strong> This method returns immediately but generates the links with
 * the specified classifier on a background thread. The generated links are applied on the
 * calling thread.
 *
 * <p><strong>Note:</strong> If the text is currently attached to a TextView, this method
 * should be called on the UI thread.
 *
 * @param text Spannable whose text is to be marked-up with links
 * @param classifier the TextClassifier to use to generate the links
 * @param params optional parameters to specify how to generate the links
 *
 * @return a future that may be used to interrupt or query the background task
 * @hide
 */
public static Future<Void> addLinksAsync(
        @NonNull Spannable text,
        @NonNull TextClassifier classifier,
        @Nullable TextLinksParams params) {
    return addLinksAsync(text, classifier, params, null /* executor */, null /* callback */);
}
 
源代码20 项目: android_9.0.0_r45   文件: Linkify.java
/**
 * Scans the text of the provided TextView and turns all occurrences of the entity types
 * specified by the link {@code mask} into clickable links. If links are found, this method
 * removes any pre-existing {@link TextLinkSpan} attached to the text to avoid
 * problems if you call it repeatedly on the same text.
 *
 * <p><strong>Note:</strong> This method returns immediately but generates the links with
 * the specified classifier on a background thread. The generated links are applied on the
 * calling thread.
 *
 * <p><strong>Note:</strong> If the text is currently attached to a TextView, this method
 * should be called on the UI thread.
 *
 * @param text Spannable whose text is to be marked-up with links
 * @param classifier the TextClassifier to use to generate the links
 * @param mask mask to define which kinds of links will be generated
 *
 * @return a future that may be used to interrupt or query the background task
 * @hide
 */
public static Future<Void> addLinksAsync(
        @NonNull Spannable text,
        @NonNull TextClassifier classifier,
        @LinkifyMask int mask) {
    return addLinksAsync(text, classifier, TextLinksParams.fromLinkMask(mask),
            null /* executor */, null /* callback */);
}
 
源代码21 项目: android_9.0.0_r45   文件: Linkify.java
/**
 * Scans the text of the provided TextView and turns all occurrences of the entity types
 * specified by {@code options} into clickable links. If links are found, this method
 * removes any pre-existing {@link TextLinkSpan} attached to the text to avoid
 * problems if you call it repeatedly on the same text.
 *
 * <p><strong>Note:</strong> This method returns immediately but generates the links with
 * the specified classifier on a background thread. The generated links are applied on the
 * calling thread.
 *
 * <p><strong>Note:</strong> If the text is currently attached to a TextView, this method
 * should be called on the UI thread.
 *
 * @param text Spannable whose text is to be marked-up with links
 * @param classifier the TextClassifier to use to generate the links
 * @param params optional parameters to specify how to generate the links
 * @param executor Executor that runs the background task
 * @param callback Callback that receives the final status of the background task execution
 *
 * @return a future that may be used to interrupt or query the background task
 * @hide
 */
public static Future<Void> addLinksAsync(
        @NonNull Spannable text,
        @NonNull TextClassifier classifier,
        @Nullable TextLinksParams params,
        @Nullable Executor executor,
        @Nullable Consumer<Integer> callback) {
    return addLinksAsync(text, classifier, params, executor, callback,
            null /* modifyTextView */);
}
 
/**
 * Creates a "selection modified" event.
 * Use when the user modifies the selection.
 *
 * @param start  the start word (inclusive) index of the selection
 * @param end  the end word (exclusive) index of the selection
 */
public static SelectionEvent selectionModified(int start, int end) {
    return new SelectionEvent(
            start, end, EventType.SELECTION_MODIFIED,
            TextClassifier.TYPE_UNKNOWN, NO_VERSION_TAG);
}
 
/**
 * Creates an event specifying an action taken on a selection.
 * Use when the user clicks on an action to act on the selected text.
 *
 * @param start  the start word (inclusive) index of the selection
 * @param end  the end word (exclusive) index of the selection
 * @param actionType  the action that was performed on the selection
 */
public static SelectionEvent selectionAction(
        int start, int end, @ActionType int actionType) {
    return new SelectionEvent(
            start, end, actionType, TextClassifier.TYPE_UNKNOWN, NO_VERSION_TAG);
}
 
 类所在包
 类方法
 同包方法