Fallback to legacy intent factory when the template is null
When RemoteActionTemplate[] is null, it means the intent generator
is not working, we then fallback to legacy intent factory
If RemoteActionTemplate[] is an empty array, it means the intent
generator is run, but no intent is generated.
Also, moved all intent related class to intent/ folder
Test: atest frameworks/base/core/tests/coretests/src/android/view/textclassifier/
Change-Id: I7e4c49dc3ebed4d498ac26f0894d7af85419419e
diff --git a/core/java/android/view/textclassifier/ActionsSuggestionsHelper.java b/core/java/android/view/textclassifier/ActionsSuggestionsHelper.java
index 17edf5c..e7ae7a0 100644
--- a/core/java/android/view/textclassifier/ActionsSuggestionsHelper.java
+++ b/core/java/android/view/textclassifier/ActionsSuggestionsHelper.java
@@ -23,10 +23,13 @@
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Pair;
+import android.view.textclassifier.intent.LabeledIntent;
+import android.view.textclassifier.intent.TemplateIntentFactory;
import com.android.internal.annotations.VisibleForTesting;
import com.google.android.textclassifier.ActionsSuggestionsModel;
+import com.google.android.textclassifier.RemoteActionTemplate;
import java.util.ArrayDeque;
import java.util.ArrayList;
@@ -46,6 +49,7 @@
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public final class ActionsSuggestionsHelper {
+ private static final String TAG = "ActionsSuggestions";
private static final int USER_LOCAL = 0;
private static final int FIRST_NON_LOCAL_USER = 1;
@@ -117,8 +121,33 @@
}
/**
- * Returns a {@link android.view.textclassifier.LabeledIntent.TitleChooser} for
- * conversation actions use case.
+ * Generated labeled intent from an action suggestion and return the resolved result.
+ */
+ @Nullable
+ public static LabeledIntent.Result createLabeledIntentResult(
+ Context context,
+ TemplateIntentFactory templateIntentFactory,
+ ActionsSuggestionsModel.ActionSuggestion nativeSuggestion) {
+ RemoteActionTemplate[] remoteActionTemplates =
+ nativeSuggestion.getRemoteActionTemplates();
+ if (remoteActionTemplates == null) {
+ Log.w(TAG, "createRemoteAction: Missing template for type "
+ + nativeSuggestion.getActionType());
+ return null;
+ }
+ List<LabeledIntent> labeledIntents = templateIntentFactory.create(remoteActionTemplates);
+ if (labeledIntents.isEmpty()) {
+ return null;
+ }
+ // Given that we only support implicit intent here, we should expect there is just one
+ // intent for each action type.
+ LabeledIntent.TitleChooser titleChooser =
+ ActionsSuggestionsHelper.createTitleChooser(nativeSuggestion.getActionType());
+ return labeledIntents.get(0).resolve(context, titleChooser);
+ }
+
+ /**
+ * Returns a {@link LabeledIntent.TitleChooser} for conversation actions use case.
*/
@Nullable
public static LabeledIntent.TitleChooser createTitleChooser(String actionType) {