Having an inconsistent issue where TextClassifier.suggestSelection() and TextClassifier.classifyText() timeout after a minute pause.
Called properly on non-main thread. Has only begun to happen after updating to Android 12 (Pixel 3). The implementation here comes from the older section of this reference: https://source.android.com/devices/tech/display/textclassifier The API seems to have developed substantially, but I don't see where the old method calls have become obsolete or incomplete.
Code to replicate:
import static android.content.Context.TEXT_CLASSIFICATION_SERVICE;
import android.content.Context;
import android.view.textclassifier.TextClassification;
import android.view.textclassifier.TextClassificationManager;
import android.view.textclassifier.TextClassifier;
import android.view.textclassifier.TextSelection;
public class TestTextClassifier {
TextClassificationManager manager;
TextClassifier classifier;
static final String TEXT = "my.email@gmail.com - detect this email address please";
public TestTextClassifier(Context context) {
System.out.println("Classifier Test - Init");
manager = (TextClassificationManager) context.getSystemService(TEXT_CLASSIFICATION_SERVICE);
classifier = manager.getTextClassifier();
}
public void test() {
new Thread(() -> {
System.out.println("Classifier Test - \""+TEXT+"\" (Length: "+TEXT.length()+")");
System.out.println("Classifier Test - Suggest Selection");
int startIndex = 0;
int endIndex = TEXT.length();
TextSelection selection = classifier.suggestSelection(TEXT, startIndex, endIndex, null);
System.out.println("Classifier Test - Selection Returned ("+selection.getSelectionStartIndex()+" - "+selection.getSelectionEndIndex()+")");
System.out.println("Classifier Test - Classify Selection");
TextClassification classification = classifier.classifyText(TEXT, selection.getSelectionStartIndex(), selection.getSelectionEndIndex(), null);
System.out.println("Classifier Test - Classification Returned ("+classification.getText()+")");
}).start();
}
}
Logcat:
2021-12-06 11:29:16.889 9261-9261/com.not.my.package I/System.out: Classifier Test - Init
2021-12-06 11:29:16.894 9261-9261/com.not.my.package D/androidtc: Initializing SystemTextClassifier, type = System
2021-12-06 11:29:16.898 9261-9842/com.not.my.package I/System.out: Classifier Test - "my.email@gmail.com - detect this email address please" (Length: 53)
2021-12-06 11:29:16.898 9261-9842/com.not.my.package I/System.out: Classifier Test - Suggest Selection
2021-12-06 11:30:16.902 9261-9842/com.not.my.package W/androidtc: Timeout in ResponseReceiver.get(): textselection
2021-12-06 11:30:16.903 9261-9842/com.not.my.package I/System.out: Classifier Test - Selection Returned (0 - 53)
2021-12-06 11:30:16.903 9261-9842/com.not.my.package I/System.out: Classifier Test - Classify Selection
2021-12-06 11:31:16.910 9261-9842/com.not.my.package W/androidtc: Timeout in ResponseReceiver.get(): textclassification
2021-12-06 11:31:16.913 9261-9842/com.not.my.package I/System.out: Classifier Test - Classification Returned (null)