From 70d41cd792cbbc1eb6b2c36be54cfcae7b53c03a Mon Sep 17 00:00:00 2001 From: Abodunrinwa Toki Date: Tue, 2 May 2017 21:43:41 +0100 Subject: [PATCH] TextClassifier: Append http:// to url intents that need one. Test: bit FrameworksCoreTests:android.view.textclassifier.TextClassificationManagerTest Bug: 36504599 Change-Id: I5008225a6a3df2d8e07e4a9ae0e340a6582f9e7b --- .../android/view/textclassifier/TextClassifierImpl.java | 4 ++++ .../textclassifier/TextClassificationManagerTest.java | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/textclassifier/TextClassifierImpl.java b/core/java/android/view/textclassifier/TextClassifierImpl.java index ebf1c0189b15..209ff097f9e6 100644 --- a/core/java/android/view/textclassifier/TextClassifierImpl.java +++ b/core/java/android/view/textclassifier/TextClassifierImpl.java @@ -607,6 +607,7 @@ final class TextClassifierImpl implements TextClassifier { @Nullable public static Intent create(Context context, String type, String text) { type = type.trim().toLowerCase(Locale.ENGLISH); + text = text.trim(); switch (type) { case TextClassifier.TYPE_EMAIL: return new Intent(Intent.ACTION_SENDTO) @@ -618,6 +619,9 @@ final class TextClassifierImpl implements TextClassifier { return new Intent(Intent.ACTION_VIEW) .setData(Uri.parse(String.format("geo:0,0?q=%s", text))); case TextClassifier.TYPE_URL: + if (!text.startsWith("https://") && !text.startsWith("http://")) { + text = "http://" + text; + } return new Intent(Intent.ACTION_VIEW, Uri.parse(text)) .putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); default: diff --git a/core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java b/core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java index f59e4fc188d4..742fd60e25f7 100644 --- a/core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java +++ b/core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java @@ -118,7 +118,7 @@ public class TextClassificationManagerTest { if (isTextClassifierDisabled()) return; String text = "Visit http://www.android.com for more information"; - String classifiedText = "http://www.android.com"; + String classifiedText = "www.android.com"; int startIndex = text.indexOf(classifiedText); int endIndex = startIndex + classifiedText.length(); assertThat(mClassifier.classifyText(text, startIndex, endIndex, LOCALES), @@ -193,7 +193,19 @@ public class TextClassificationManagerTest { public boolean matches(Object o) { if (o instanceof TextClassification) { TextClassification result = (TextClassification) o; - return text.equals(result.getText()) + final boolean typeRequirementSatisfied; + switch (type) { + case TextClassifier.TYPE_URL: + String scheme = result.getIntent().getData().getScheme(); + typeRequirementSatisfied = "http".equalsIgnoreCase(scheme) + || "https".equalsIgnoreCase(scheme); + break; + default: + typeRequirementSatisfied = true; + } + + return typeRequirementSatisfied + && text.equals(result.getText()) && result.getEntityCount() > 0 && type.equals(result.getEntity(0)); // TODO: Include other properties. -- 2.11.0