android.view.textclassifier.TextClassifier Java Examples

The following examples show how to use android.view.textclassifier.TextClassifier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmartSelectionEventTracker.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #2
Source File: SelectionActionModeHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #3
Source File: ApiCompatibilityUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: SmartSelectionEventTracker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #5
Source File: SelectionActionModeHelper.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #6
Source File: SmartSelectionEventTracker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #7
Source File: SelectionActionModeHelper.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #8
Source File: TextClassifierService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #9
Source File: WebView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example #10
Source File: TEditText.java    From timecat with Apache License 2.0 4 votes vote down vote up
@NonNull
@Override
public TextClassifier getTextClassifier() {
    return super.getTextClassifier();
}
 
Example #11
Source File: TEditText.java    From timecat with Apache License 2.0 4 votes vote down vote up
@Override
public void setTextClassifier(@Nullable TextClassifier textClassifier) {
    super.setTextClassifier(textClassifier);
}
 
Example #12
Source File: SelectionActionModeHelper.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
TextClassificationHelper(Context context, Supplier<TextClassifier> textClassifier,
        CharSequence text, int selectionStart, int selectionEnd, LocaleList locales) {
    init(textClassifier, text, selectionStart, selectionEnd, locales);
    mContext = Preconditions.checkNotNull(context);
}
 
Example #13
Source File: Linkify.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
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);
    }
}
 
Example #14
Source File: SmartSelectionEventTracker.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #15
Source File: WebViewProvider.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@NonNull
public default TextClassifier getTextClassifier() { return TextClassifier.NO_OP; }
 
Example #16
Source File: WebViewProvider.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
public default void setTextClassifier(@Nullable TextClassifier textClassifier) {}
 
Example #17
Source File: WebView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the {@link TextClassifier} for this WebView.
 */
public void setTextClassifier(@Nullable TextClassifier textClassifier) {
    mProvider.setTextClassifier(textClassifier);
}
 
Example #18
Source File: Linkify.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * 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 */);
}
 
Example #19
Source File: Linkify.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * 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 */);
}
 
Example #20
Source File: Linkify.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * 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 */);
}
 
Example #21
Source File: SmartSelectionEventTracker.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example #22
Source File: SmartSelectionEventTracker.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #23
Source File: SmartSelectionEventTracker.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}