Merge "TextClassifierImpl: Handle urls."

This commit is contained in:
Abodunrinwa Toki
2017-02-13 17:35:14 +00:00
committed by Android (Google) Code Review
9 changed files with 41 additions and 18 deletions

View File

@@ -47339,6 +47339,7 @@ package android.view.textclassifier {
field public static final java.lang.String TYPE_EMAIL = "email";
field public static final java.lang.String TYPE_OTHER = "other";
field public static final java.lang.String TYPE_PHONE = "phone";
field public static final java.lang.String TYPE_URL = "url";
}
public static abstract class TextClassifier.EntityType implements java.lang.annotation.Annotation {

View File

@@ -50778,6 +50778,7 @@ package android.view.textclassifier {
field public static final java.lang.String TYPE_EMAIL = "email";
field public static final java.lang.String TYPE_OTHER = "other";
field public static final java.lang.String TYPE_PHONE = "phone";
field public static final java.lang.String TYPE_URL = "url";
}
public static abstract class TextClassifier.EntityType implements java.lang.annotation.Annotation {

View File

@@ -47652,6 +47652,7 @@ package android.view.textclassifier {
field public static final java.lang.String TYPE_EMAIL = "email";
field public static final java.lang.String TYPE_OTHER = "other";
field public static final java.lang.String TYPE_PHONE = "phone";
field public static final java.lang.String TYPE_URL = "url";
}
public static abstract class TextClassifier.EntityType implements java.lang.annotation.Annotation {

View File

@@ -148,7 +148,7 @@ public final class TextClassificationResult {
* @hide
*/
@NonNull
public static OnClickListener createStartActivityOnClick(
public static OnClickListener createStartActivityOnClickListener(
@NonNull final Context context, @NonNull final Intent intent) {
Preconditions.checkArgument(context != null);
Preconditions.checkArgument(intent != null);

View File

@@ -35,6 +35,7 @@ public interface TextClassifier {
String TYPE_EMAIL = "email";
String TYPE_PHONE = "phone";
String TYPE_ADDRESS = "address";
String TYPE_URL = "url";
@Retention(RetentionPolicy.SOURCE)
@StringDef({

View File

@@ -27,6 +27,7 @@ import android.graphics.drawable.Drawable;
import android.icu.text.BreakIterator;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.Browser;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.WordIterator;
@@ -44,6 +45,7 @@ import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
@@ -112,7 +114,7 @@ final class TextClassifierImpl implements TextClassifier {
String type = getSmartSelection()
.classifyText(text.toString(), startIndex, endIndex);
if (!TextUtils.isEmpty(type)) {
type = type.toLowerCase().trim();
type = type.toLowerCase(Locale.ENGLISH).trim();
// TODO: Added this log for debug only. Remove before release.
Log.d(LOG_TAG, String.format("Classification type: %s", type));
return createClassificationResult(type, classified);
@@ -126,7 +128,6 @@ final class TextClassifierImpl implements TextClassifier {
return TextClassifier.NO_OP.getTextClassificationResult(text, startIndex, endIndex);
}
@Override
public LinksInfo getLinks(CharSequence text, int linkMask) {
Preconditions.checkArgument(text != null);
@@ -151,20 +152,25 @@ final class TextClassifierImpl implements TextClassifier {
}
private TextClassificationResult createClassificationResult(String type, CharSequence text) {
final Intent intent = IntentFactory.create(type, text.toString());
if (intent == null) {
return TextClassificationResult.EMPTY;
}
final TextClassificationResult.Builder builder = new TextClassificationResult.Builder()
.setText(text.toString())
.setEntityType(type, 1.0f /* confidence */)
.setIntent(intent)
.setOnClickListener(TextClassificationResult.createStartActivityOnClick(
mContext, intent));
final PackageManager pm = mContext.getPackageManager();
final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
.setEntityType(type, 1.0f /* confidence */);
final Intent intent = IntentFactory.create(mContext, type, text.toString());
final PackageManager pm;
final ResolveInfo resolveInfo;
if (intent != null) {
pm = mContext.getPackageManager();
resolveInfo = pm.resolveActivity(intent, 0);
} else {
pm = null;
resolveInfo = null;
}
if (resolveInfo != null && resolveInfo.activityInfo != null) {
builder.setIntent(intent)
.setOnClickListener(TextClassificationResult.createStartActivityOnClickListener(
mContext, intent));
final String packageName = resolveInfo.activityInfo.packageName;
if ("android".equals(packageName)) {
// Requires the chooser to find an activity to handle the intent.
@@ -227,7 +233,7 @@ final class TextClassifierImpl implements TextClassifier {
smartSelection.classifyText(text, selectionStart, selectionEnd);
if (matches(type, linkMask)) {
final Intent intent = IntentFactory.create(
type, text.substring(selectionStart, selectionEnd));
context, type, text.substring(selectionStart, selectionEnd));
if (hasActivityHandler(context, intent)) {
final ClickableSpan span = createSpan(context, intent);
spans.add(new SpanSpec(selectionStart, selectionEnd, span));
@@ -255,6 +261,10 @@ final class TextClassifierImpl implements TextClassifier {
&& TextClassifier.TYPE_ADDRESS.equals(type)) {
return true;
}
if ((linkMask & Linkify.WEB_URLS) != 0
&& TextClassifier.TYPE_URL.equals(type)) {
return true;
}
return false;
}
@@ -369,7 +379,7 @@ final class TextClassifierImpl implements TextClassifier {
private IntentFactory() {}
@Nullable
public static Intent create(String type, String text) {
public static Intent create(Context context, String type, String text) {
switch (type) {
case TextClassifier.TYPE_EMAIL:
return new Intent(Intent.ACTION_SENDTO)
@@ -380,6 +390,9 @@ final class TextClassifierImpl implements TextClassifier {
case TextClassifier.TYPE_ADDRESS:
return new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse(String.format("geo:0,0?q=%s", text)));
case TextClassifier.TYPE_URL:
return new Intent(Intent.ACTION_VIEW, Uri.parse(text))
.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
default:
return null;
// TODO: Add other classification types.
@@ -395,6 +408,8 @@ final class TextClassifierImpl implements TextClassifier {
return context.getString(com.android.internal.R.string.dial);
case TextClassifier.TYPE_ADDRESS:
return context.getString(com.android.internal.R.string.map);
case TextClassifier.TYPE_URL:
return context.getString(com.android.internal.R.string.browse);
default:
return null;
// TODO: Add other classification types.

View File

@@ -3902,7 +3902,7 @@ public class Editor {
} else {
final Intent intent = textClassificationResult.getIntent();
if (intent != null) {
TextClassificationResult.createStartActivityOnClick(
TextClassificationResult.createStartActivityOnClickListener(
mTextView.getContext(), intent)
.onClick(mTextView);
}

View File

@@ -2602,11 +2602,14 @@
<string name="email">Email</string>
<!-- Label for item in the text selection menu to trigger a Dialer app [CHAR LIMIT=20] -->
<string name="dial">Dial</string>
<string name="dial">Phone</string>
<!-- Label for item in the text selection menu to trigger a Map app [CHAR LIMIT=20] -->
<string name="map">Map</string>
<!-- Label for item in the text selection menu to trigger a Browser app [CHAR LIMIT=20] -->
<string name="browse">Browse</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. -->

View File

@@ -482,6 +482,7 @@
<java-symbol type="string" name="email" />
<java-symbol type="string" name="dial" />
<java-symbol type="string" name="map" />
<java-symbol type="string" name="browse" />
<java-symbol type="string" name="textSelectionCABTitle" />
<java-symbol type="string" name="BaMmi" />
<java-symbol type="string" name="CLIRDefaultOffNextCallOff" />