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

Commit aa0fc10c authored by Abodunrinwa Toki's avatar Abodunrinwa Toki Committed by Android (Google) Code Review
Browse files

Merge "TextClassifier: normalize uri for browser intent."

parents 4907fccc c33fc77d
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -85,15 +85,16 @@ public final class ExtrasUtils {
    }

    /**
     * Returns the first "translate" action found in the {@code classification} object.
     * Returns the first action found in the {@code classification} object with an intent
     * action string, {@code intentAction}.
     */
    @Nullable
    public static RemoteAction findTranslateAction(TextClassification classification) {
    public static RemoteAction findAction(TextClassification classification, String intentAction) {
        final ArrayList<Intent> actionIntents = getActionsIntents(classification);
        if (actionIntents != null) {
            final int size = actionIntents.size();
            for (int i = 0; i < size; i++) {
                if (Intent.ACTION_TRANSLATE.equals(actionIntents.get(i).getAction())) {
                if (intentAction.equals(actionIntents.get(i).getAction())) {
                    return classification.getActions().get(i);
                }
            }
@@ -101,6 +102,14 @@ public final class ExtrasUtils {
        return null;
    }

    /**
     * Returns the first "translate" action found in the {@code classification} object.
     */
    @Nullable
    public static RemoteAction findTranslateAction(TextClassification classification) {
        return findAction(classification, Intent.ACTION_TRANSLATE);
    }

    /**
     * Returns the entity type contained in the {@code extra}.
     */
+2 −1
Original line number Diff line number Diff line
@@ -182,7 +182,8 @@ public final class LegacyIntentFactory implements IntentFactory {
        actions.add(new LabeledIntent(
                context.getString(com.android.internal.R.string.browse),
                context.getString(com.android.internal.R.string.browse_desc),
                new Intent(Intent.ACTION_VIEW, Uri.parse(text))
                new Intent(Intent.ACTION_VIEW)
                        .setDataAndNormalize(Uri.parse(text))
                        .putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()),
                LabeledIntent.DEFAULT_REQUEST_CODE));
        return actions;
+43 −28
Original line number Diff line number Diff line
@@ -49,20 +49,18 @@ public final class TemplateIntentFactory {
        }
        final List<TextClassifierImpl.LabeledIntent> labeledIntents = new ArrayList<>();
        for (RemoteActionTemplate remoteActionTemplate : remoteActionTemplates) {
            Intent intent = createIntent(remoteActionTemplate);
            if (intent == null) {
            if (!isValidTemplate(remoteActionTemplate)) {
                Log.w(TAG, "Invalid RemoteActionTemplate skipped.");
                continue;
            }
            TextClassifierImpl.LabeledIntent
                    labeledIntent = new TextClassifierImpl.LabeledIntent(
            labeledIntents.add(
                    new TextClassifierImpl.LabeledIntent(
                            remoteActionTemplate.title,
                            remoteActionTemplate.description,
                    intent,
                            createIntent(remoteActionTemplate),
                            remoteActionTemplate.requestCode == null
                                    ? TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE
                            : remoteActionTemplate.requestCode
            );
            labeledIntents.add(labeledIntent);
                                    : remoteActionTemplate.requestCode));
        }
        labeledIntents.forEach(
                action -> action.getIntent()
@@ -70,31 +68,45 @@ public final class TemplateIntentFactory {
        return labeledIntents;
    }

    @Nullable
    private static Intent createIntent(RemoteActionTemplate remoteActionTemplate) {
        Intent intent = new Intent();
        if (!TextUtils.isEmpty(remoteActionTemplate.packageName)) {
            Log.w(TAG, "A RemoteActionTemplate is skipped as package name is set.");
            return null;
    private static boolean isValidTemplate(@Nullable RemoteActionTemplate remoteActionTemplate) {
        if (remoteActionTemplate == null) {
            Log.w(TAG, "Invalid RemoteActionTemplate: is null");
            return false;
        }
        if (!TextUtils.isEmpty(remoteActionTemplate.action)) {
            intent.setAction(remoteActionTemplate.action);
        if (TextUtils.isEmpty(remoteActionTemplate.title)) {
            Log.w(TAG, "Invalid RemoteActionTemplate: title is null");
            return false;
        }
        Uri data = null;
        if (!TextUtils.isEmpty(remoteActionTemplate.data)) {
            data = Uri.parse(remoteActionTemplate.data);
        if (TextUtils.isEmpty(remoteActionTemplate.description)) {
            Log.w(TAG, "Invalid RemoteActionTemplate: description is null");
            return false;
        }
        if (data != null || !TextUtils.isEmpty(remoteActionTemplate.type)) {
            intent.setDataAndType(data, remoteActionTemplate.type);
        if (!TextUtils.isEmpty(remoteActionTemplate.packageName)) {
            Log.w(TAG, "Invalid RemoteActionTemplate: package name is set");
            return false;
        }
        if (remoteActionTemplate.flags != null) {
            intent.setFlags(remoteActionTemplate.flags);
        if (TextUtils.isEmpty(remoteActionTemplate.action)) {
            Log.w(TAG, "Invalid RemoteActionTemplate: intent action not set");
            return false;
        }
        return true;
    }

    private static Intent createIntent(RemoteActionTemplate remoteActionTemplate) {
        final Intent intent = new Intent(remoteActionTemplate.action);
        final Uri uri = TextUtils.isEmpty(remoteActionTemplate.data)
                ? null : Uri.parse(remoteActionTemplate.data).normalizeScheme();
        final String type = TextUtils.isEmpty(remoteActionTemplate.type)
                ? null : Intent.normalizeMimeType(remoteActionTemplate.type);
        intent.setDataAndType(uri, type);
        intent.setFlags(remoteActionTemplate.flags == null ? 0 : remoteActionTemplate.flags);
        if (remoteActionTemplate.category != null) {
            for (String category : remoteActionTemplate.category) {
                if (category != null) {
                    intent.addCategory(category);
                }
            }
        }
        intent.putExtras(createExtras(remoteActionTemplate.extras));
        return intent;
    }
@@ -105,6 +117,9 @@ public final class TemplateIntentFactory {
        }
        Bundle bundle = new Bundle();
        for (NamedVariant namedVariant : namedVariants) {
            if (namedVariant == null) {
                continue;
            }
            switch (namedVariant.getType()) {
                case NamedVariant.TYPE_INT:
                    bundle.putInt(namedVariant.getName(), namedVariant.getInt());
+20 −28
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ public class TemplateClassificationIntentFactoryTest {

    private static final String TEXT = "text";
    private static final String TITLE = "Map";
    private static final String DESCRIPTION = "Opens in Maps";
    private static final String ACTION = Intent.ACTION_VIEW;

    @Mock
@@ -57,19 +58,6 @@ public class TemplateClassificationIntentFactoryTest {

    @Test
    public void create_foreignText() {
        RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate(
                TITLE,
                null,
                ACTION,
                null,
                null,
                null,
                null,
                null,
                null,
                null
        );

        AnnotatorModel.ClassificationResult classificationResult =
                new AnnotatorModel.ClassificationResult(
                        TextClassifier.TYPE_ADDRESS,
@@ -81,7 +69,7 @@ public class TemplateClassificationIntentFactoryTest {
                        null,
                        null,
                        null,
                        new RemoteActionTemplate[]{remoteActionTemplate});
                        createRemoteActionTemplates());

        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateClassificationIntentFactory.create(
@@ -106,19 +94,6 @@ public class TemplateClassificationIntentFactoryTest {

    @Test
    public void create_notForeignText() {
        RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate(
                TITLE,
                null,
                ACTION,
                null,
                null,
                null,
                null,
                null,
                null,
                null
        );

        AnnotatorModel.ClassificationResult classificationResult =
                new AnnotatorModel.ClassificationResult(
                        TextClassifier.TYPE_ADDRESS,
@@ -130,7 +105,7 @@ public class TemplateClassificationIntentFactoryTest {
                        null,
                        null,
                        null,
                        new RemoteActionTemplate[]{remoteActionTemplate});
                        createRemoteActionTemplates());

        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateClassificationIntentFactory.create(
@@ -147,4 +122,21 @@ public class TemplateClassificationIntentFactoryTest {
        assertThat(intent.getAction()).isEqualTo(ACTION);
        assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue();
    }

    private static RemoteActionTemplate[] createRemoteActionTemplates() {
        return new RemoteActionTemplate[]{
                new RemoteActionTemplate(
                        TITLE,
                        DESCRIPTION,
                        ACTION,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null
                )
        };
    }
}
+106 −14
Original line number Diff line number Diff line
@@ -81,7 +81,6 @@ public class TemplateIntentFactoryTest {
                REQUEST_CODE
        );


        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate});

@@ -97,23 +96,22 @@ public class TemplateIntentFactoryTest {
        assertThat(intent.getFlags()).isEqualTo(FLAG);
        assertThat(intent.getCategories()).containsExactly((Object[]) CATEGORY);
        assertThat(intent.getPackage()).isNull();
        assertThat(
                intent.getStringExtra(KEY_ONE)).isEqualTo(VALUE_ONE);
        assertThat(intent.getStringExtra(KEY_ONE)).isEqualTo(VALUE_ONE);
        assertThat(intent.getIntExtra(KEY_TWO, 0)).isEqualTo(VALUE_TWO);
        assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue();
    }

    @Test
    public void create_packageIsNotNull() {
    public void normalizesScheme() {
        RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate(
                TITLE,
                DESCRIPTION,
                ACTION,
                DATA,
                "HTTp://www.android.com",
                TYPE,
                FLAG,
                CATEGORY,
                PACKAGE_NAME,
                /* packageName */ null,
                NAMED_VARIANTS,
                REQUEST_CODE
        );
@@ -121,15 +119,16 @@ public class TemplateIntentFactoryTest {
        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate});

        assertThat(intents).hasSize(0);
        String data = intents.get(0).getIntent().getData().toString();
        assertThat(data).isEqualTo("http://www.android.com");
    }

    @Test
    public void create_minimal() {
        RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate(
                null,
                null,
                null,
                TITLE,
                DESCRIPTION,
                ACTION,
                null,
                null,
                null,
@@ -142,15 +141,14 @@ public class TemplateIntentFactoryTest {
        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate});


        assertThat(intents).hasSize(1);
        TextClassifierImpl.LabeledIntent labeledIntent = intents.get(0);
        assertThat(labeledIntent.getTitle()).isNull();
        assertThat(labeledIntent.getDescription()).isNull();
        assertThat(labeledIntent.getTitle()).isEqualTo(TITLE);
        assertThat(labeledIntent.getDescription()).isEqualTo(DESCRIPTION);
        assertThat(labeledIntent.getRequestCode()).isEqualTo(
                TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE);
        Intent intent = labeledIntent.getIntent();
        assertThat(intent.getAction()).isNull();
        assertThat(intent.getAction()).isEqualTo(ACTION);
        assertThat(intent.getData()).isNull();
        assertThat(intent.getType()).isNull();
        assertThat(intent.getFlags()).isEqualTo(0);
@@ -158,4 +156,98 @@ public class TemplateIntentFactoryTest {
        assertThat(intent.getPackage()).isNull();
        assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue();
    }

    @Test
    public void invalidTemplate_nullTemplate() {
        RemoteActionTemplate remoteActionTemplate = null;

        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate});

        assertThat(intents).isEmpty();
    }

    @Test
    public void invalidTemplate_nonEmptyPackageName() {
        RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate(
                TITLE,
                DESCRIPTION,
                ACTION,
                DATA,
                TYPE,
                FLAG,
                CATEGORY,
                PACKAGE_NAME,
                NAMED_VARIANTS,
                REQUEST_CODE
        );

        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate});

        assertThat(intents).isEmpty();
    }

    @Test
    public void invalidTemplate_emptyTitle() {
        RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate(
                null,
                DESCRIPTION,
                ACTION,
                null,
                null,
                null,
                null,
                null,
                null,
                null
        );

        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate});

        assertThat(intents).isEmpty();
    }

    @Test
    public void invalidTemplate_emptyDescription() {
        RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate(
                TITLE,
                null,
                ACTION,
                null,
                null,
                null,
                null,
                null,
                null,
                null
        );

        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate});

        assertThat(intents).isEmpty();
    }

    @Test
    public void invalidTemplate_emptyIntentAction() {
        RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate(
                TITLE,
                DESCRIPTION,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
        );

        List<TextClassifierImpl.LabeledIntent> intents =
                mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate});

        assertThat(intents).isEmpty();
    }
}
Loading