TextClassification – Part 3

In API 26 (Oreo) a new TextClassification system was introduced. This has been further refined in API 28 (Pie). In this short series we’ll take a look at what this is, how to use it, and how we can add custom behaviours to it.

Previously we began work on a custom TextClassifier implementation and looked at how we can implement our own selection suggestion implementation. In this final article in this series, we’ll implement the corresponding classifyText() and hook up our custom TextClassifier implementation.

The classifyText() implementation is actually pretty straightforward. We much check that the selection matches the selection in the request, and if so we return a TextClassification instance which we’ll look at in a moment. If the selection does not match our regex, then we defer to the fallback (which is the default implementation of TextClassifier):

There’s an extension function to TextClassification.Request which will return the subSequence of the text that has been selected, and we use this to match against the regex (line 36). We then build a TextClassification instance which requires the matches string (line 38), a list of classification types and their respective confidence scores (line 39), and a list of RemoteAction instances each of which corresponds to one of classification type entries (lines 39-45).

The buildRemoteAction() method constructs each RemoteAction instance:

TextClassifierFactory.kt

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

override fun buildRemoteAction(

context:Context,

drawableId:Int,

title:String,

contentDescription:String,

uri:String

):RemoteAction{

returnRemoteAction(

Icon.createWithResource(context,drawableId),

title,

contentDescription,

PendingIntent.getActivity(

context,

0,

Intent(Intent.ACTION_VIEW,Uri.parse(uri)),

0

)

)

}

The RemoteAction constructor takes four arguments: An Icon which will be displayed as part of the button which provides the action; The text to be displayed; a content description for accessibility purposes, and finally a PendingIntent which represents the action that will be performed if the user taps on the action button. In our example code we use a drawable of the Styling Android logo for the icon, the text “Styling Android” for the title, and simple content description, and a PendingIntent which will launch a browser with the URL “https://blog.stylingandroid.com”.

The buildTextClassification() function uses a TextClassification.Builder instance to create the TextClassification instance:

TextClassifierFactory.kt

54

55

56

57

58

59

60

61

62

63

64

65

66

override fun buildTextClassification(

text:String,

entityTypes:List<Pair<String,Float>>,

actions:List<RemoteAction>

):TextClassification{

returnTextClassification.Builder()

.run{

setText(text)

entityTypes.forEach{setEntityType(it.first,it.second)}

actions.forEach{addAction(it)}

build()

}

}

This takes three arguments: The text that was matched, a list of types and their confidence scopes as Pairs, and the list of RemoteActions corresponding to each type / confidence Pair. In the example these will be the selected substring, a list containing one Pair of TextClassifier.TYPE_URL with a confidence score of 1.0f, and a singleton list containing the RemoteAction that we just looked at.

Thats the custom TextClassifier completed, and all that remains is to hook it up to our TextView:

A single line of code does everything we need. We create an instance of our custom TextClassifier and pass it an instance of the default TextClassifier as a constructor argument, and set the textClassifier of the TextView to our custom instance.

The behaviour that we now get is that if the user long presses on unrecognised types there will be simple copy, paste, and select all options; if they long press within any of the types supported by default TextClassifier then they get all of the same behaviours and actions as we saw in the first article; but if they long press on the string “Styling Android” (or variants matching the regex) then we get the custom, branded action which will launch the browser and load https://blog.stylingandroid.com:

There is also a mechanism in TextClassifier to identify concrete types, and then generate links within the text, but we’ll skip that as part of this series as the main functionality built in to TextView and WebView is covered by the techniques that we’ve looked at here. If anyone is interested in this, then please let me know and I’ll write a separate article detailing it.