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

Commit e1f3ac06 authored by Tony Mak's avatar Tony Mak
Browse files

Add "dictionary" entity type

We will classify the word as "dictionary" if we think user may want to look
it up. A "Dictionary" option will be shown in the floating toolbar
if users select words of this entity type.

A new Intent action is introduced to bring up the dictionary app.

The model that supports this entity type is not checked-in yet.
So no behavior change can be seen for now.

Test: Check-in the WIP model and try to select a "dictionary word".
Test: atest frameworks/base/core/tests/coretests/src/android/view/textclassifier/IntentFactoryTest.java

BUG: 68238822

Change-Id: Ib318d5b2abf79d61e55b33e397065a0714573121
parent 060248c3
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10043,6 +10043,7 @@ package android.content {
    field public static final java.lang.String ACTION_CREATE_SHORTCUT = "android.intent.action.CREATE_SHORTCUT";
    field public static final java.lang.String ACTION_DATE_CHANGED = "android.intent.action.DATE_CHANGED";
    field public static final java.lang.String ACTION_DEFAULT = "android.intent.action.VIEW";
    field public static final java.lang.String ACTION_DEFINE = "android.intent.action.DEFINE";
    field public static final java.lang.String ACTION_DELETE = "android.intent.action.DELETE";
    field public static final deprecated java.lang.String ACTION_DEVICE_STORAGE_LOW = "android.intent.action.DEVICE_STORAGE_LOW";
    field public static final deprecated java.lang.String ACTION_DEVICE_STORAGE_OK = "android.intent.action.DEVICE_STORAGE_OK";
+10 −0
Original line number Diff line number Diff line
@@ -3600,6 +3600,16 @@ public class Intent implements Parcelable, Cloneable {
    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
    public static final String ACTION_TRANSLATE = "android.intent.action.TRANSLATE";

    /**
     * Activity Action: Define the meaning of the selected word(s).
     * <p>
     * Input: {@link #EXTRA_TEXT getCharSequence(EXTRA_TEXT)} is the text to define.
     * <p>
     * Output: nothing.
     */
    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
    public static final String ACTION_DEFINE = "android.intent.action.DEFINE";

    /**
     * Broadcast Action: List of dynamic sensor is changed due to new sensor being connected or
     * exisiting sensor being disconnected.
+6 −0
Original line number Diff line number Diff line
@@ -90,6 +90,11 @@ public interface TextClassifier {
    String TYPE_DATE_TIME = "datetime";
    /** Flight number in IATA format. */
    String TYPE_FLIGHT_NUMBER = "flight";
    /**
     * Word that users may be interested to look up for meaning.
     * @hide
     */
    String TYPE_DICTIONARY = "dictionary";

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
@@ -103,6 +108,7 @@ public interface TextClassifier {
            TYPE_DATE,
            TYPE_DATE_TIME,
            TYPE_FLIGHT_NUMBER,
            TYPE_DICTIONARY
    })
    @interface EntityType {}

+21 −3
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.provider.CalendarContract;
import android.provider.ContactsContract;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.util.Preconditions;

@@ -57,6 +58,7 @@ import java.net.URLEncoder;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -638,7 +640,8 @@ public final class TextClassifierImpl implements TextClassifier {
    /**
     * Helper class to store the information from which RemoteActions are built.
     */
    private static final class LabeledIntent {
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    public static final class LabeledIntent {

        static final int DEFAULT_REQUEST_CODE = 0;

@@ -673,7 +676,8 @@ public final class TextClassifierImpl implements TextClassifier {
            return mDescription;
        }

        Intent getIntent() {
        @VisibleForTesting
        public Intent getIntent() {
            return mIntent;
        }

@@ -717,7 +721,8 @@ public final class TextClassifierImpl implements TextClassifier {
    /**
     * Creates intents based on the classification type.
     */
    static final class IntentFactory {
    @VisibleForTesting
    public static final class IntentFactory {

        private static final long MIN_EVENT_FUTURE_MILLIS = TimeUnit.MINUTES.toMillis(5);
        private static final long DEFAULT_EVENT_DURATION = TimeUnit.HOURS.toMillis(1);
@@ -762,6 +767,9 @@ public final class TextClassifierImpl implements TextClassifier {
                case TextClassifier.TYPE_FLIGHT_NUMBER:
                    actions = createForFlight(context, text);
                    break;
                case TextClassifier.TYPE_DICTIONARY:
                    actions = createForDictionary(context, text);
                    break;
                default:
                    actions = new ArrayList<>();
                    break;
@@ -924,5 +932,15 @@ public final class TextClassifierImpl implements TextClassifier {
                            .putExtra(Intent.EXTRA_TEXT, text),
                    text.hashCode()));
        }

        @NonNull
        private static List<LabeledIntent> createForDictionary(Context context, String text) {
            return Arrays.asList(new LabeledIntent(
                    context.getString(com.android.internal.R.string.define),
                    context.getString(com.android.internal.R.string.define_desc),
                    new Intent(Intent.ACTION_DEFINE)
                            .putExtra(Intent.EXTRA_TEXT, text),
                    text.hashCode()));
        }
    }
}
+7 −1
Original line number Diff line number Diff line
@@ -2995,6 +2995,12 @@
    <!-- Accessibility description for an item in the text selection menu to translate selected text with a translation app. [CHAR LIMIT=NONE] -->
    <string name="translate_desc">Translate selected text</string>

    <!-- Label for item in the text selection menu to define selected text with a dictionary app. Should be a verb. [CHAR LIMIT=30] -->
    <string name="define">Define</string>

    <!-- Accessibility description for an item in the text selection menu to define selected text with a dictionary app. Should be a verb. [CHAR LIMIT=NONE] -->
    <string name="define_desc">Define selected text</string>

    <!-- If the device is getting low on internal storage, a notification is shown to the user.  This is the title of that notification. -->
    <string name="low_internal_storage_view_title">Storage space running out</string>
    <!-- If the device is getting low on internal storage, a notification is shown to the user.  This is the message of that notification. -->
Loading