summaryrefslogtreecommitdiff
path: root/core/java
diff options
context:
space:
mode:
authorAbodunrinwa Toki <toki@google.com>2019-02-08 21:39:09 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2019-02-08 21:39:09 +0000
commitaa0fc10c7aaee6117a87540488a6780e04e06456 (patch)
tree127e981643254cd99b3a6ff4c7cbac89ccb2b80e /core/java
parent4907fccc02edfb88dce7930ed3a2c8b6c6a3ad05 (diff)
parentc33fc77de4abfcc51a2a1be3c5c5e3be510f26a3 (diff)
Merge "TextClassifier: normalize uri for browser intent."
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/view/textclassifier/ExtrasUtils.java15
-rw-r--r--core/java/android/view/textclassifier/LegacyIntentFactory.java3
-rw-r--r--core/java/android/view/textclassifier/TemplateIntentFactory.java71
3 files changed, 57 insertions, 32 deletions
diff --git a/core/java/android/view/textclassifier/ExtrasUtils.java b/core/java/android/view/textclassifier/ExtrasUtils.java
index 602455c65beb..b0e7ad5d7264 100644
--- a/core/java/android/view/textclassifier/ExtrasUtils.java
+++ b/core/java/android/view/textclassifier/ExtrasUtils.java
@@ -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);
}
}
@@ -102,6 +103,14 @@ public final class ExtrasUtils {
}
/**
+ * 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}.
*/
@Nullable
diff --git a/core/java/android/view/textclassifier/LegacyIntentFactory.java b/core/java/android/view/textclassifier/LegacyIntentFactory.java
index b6e5b3e26b16..2d0d032cfef3 100644
--- a/core/java/android/view/textclassifier/LegacyIntentFactory.java
+++ b/core/java/android/view/textclassifier/LegacyIntentFactory.java
@@ -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;
diff --git a/core/java/android/view/textclassifier/TemplateIntentFactory.java b/core/java/android/view/textclassifier/TemplateIntentFactory.java
index 52788436b3ea..95f88c7de146 100644
--- a/core/java/android/view/textclassifier/TemplateIntentFactory.java
+++ b/core/java/android/view/textclassifier/TemplateIntentFactory.java
@@ -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(
- remoteActionTemplate.title,
- remoteActionTemplate.description,
- intent,
- remoteActionTemplate.requestCode == null
- ? TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE
- : remoteActionTemplate.requestCode
- );
- labeledIntents.add(labeledIntent);
+ labeledIntents.add(
+ new TextClassifierImpl.LabeledIntent(
+ remoteActionTemplate.title,
+ remoteActionTemplate.description,
+ createIntent(remoteActionTemplate),
+ remoteActionTemplate.requestCode == null
+ ? TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE
+ : remoteActionTemplate.requestCode));
}
labeledIntents.forEach(
action -> action.getIntent()
@@ -70,29 +68,43 @@ 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) {
- intent.addCategory(category);
+ if (category != null) {
+ intent.addCategory(category);
+ }
}
}
intent.putExtras(createExtras(remoteActionTemplate.extras));
@@ -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());