Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8ab9b188 authored by Tony Mak's avatar Tony Mak
Browse files

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
parent 6983ad3f
Loading
Loading
Loading
Loading
+31 −2
Original line number Diff line number Diff line
@@ -23,10 +23,13 @@ import android.content.Context;
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 @@ import java.util.stream.Collectors;
 */
@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 @@ public final class ActionsSuggestionsHelper {
    }

    /**
     * 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) {
+3 −1
Original line number Diff line number Diff line
@@ -22,8 +22,10 @@ package android.view.textclassifier;
 * To enable full log:
 * 1. adb shell setprop log.tag.androidtc VERBOSE
 * 2. adb shell stop && adb shell start
 *
 * @hide
 */
final class Log {
public final class Log {

    /**
     * true: Enables full logging.
+19 −18
Original line number Diff line number Diff line
@@ -25,6 +25,11 @@ import android.icu.util.ULocale;
import android.os.Bundle;
import android.os.LocaleList;
import android.os.ParcelFileDescriptor;
import android.view.textclassifier.intent.ClassificationIntentFactory;
import android.view.textclassifier.intent.LabeledIntent;
import android.view.textclassifier.intent.LegacyClassificationIntentFactory;
import android.view.textclassifier.intent.TemplateClassificationIntentFactory;
import android.view.textclassifier.intent.TemplateIntentFactory;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.IndentingPrintWriter;
@@ -110,7 +115,7 @@ public final class TextClassifierImpl implements TextClassifier {
    private final ModelFileManager mLangIdModelFileManager;
    private final ModelFileManager mActionsModelFileManager;

    private final IntentFactory mIntentFactory;
    private final ClassificationIntentFactory mClassificationIntentFactory;
    private final TemplateIntentFactory mTemplateIntentFactory;

    public TextClassifierImpl(
@@ -142,10 +147,10 @@ public final class TextClassifierImpl implements TextClassifier {
                        ActionsSuggestionsModel::getLocales));

        mTemplateIntentFactory = new TemplateIntentFactory();
        mIntentFactory = mSettings.isTemplateIntentFactoryEnabled()
        mClassificationIntentFactory = mSettings.isTemplateIntentFactoryEnabled()
                ? new TemplateClassificationIntentFactory(
                mTemplateIntentFactory, new LegacyIntentFactory())
                : new LegacyIntentFactory();
                mTemplateIntentFactory, new LegacyClassificationIntentFactory())
                : new LegacyClassificationIntentFactory();
    }

    public TextClassifierImpl(Context context, TextClassificationConstants settings) {
@@ -435,20 +440,16 @@ public final class TextClassifierImpl implements TextClassifier {
            if (!expectedTypes.contains(actionType)) {
                continue;
            }
            List<LabeledIntent> labeledIntents =
                    mTemplateIntentFactory.create(nativeSuggestion.getRemoteActionTemplates());
            Bundle extras = new Bundle();
            LabeledIntent.Result labeledIntentResult =
                    ActionsSuggestionsHelper.createLabeledIntentResult(
                            mContext,
                            mTemplateIntentFactory,
                            nativeSuggestion);
            RemoteAction remoteAction = null;
            // Given that we only support implicit intent here, we should expect there is just one
            // intent for each action type.
            if (!labeledIntents.isEmpty()) {
                LabeledIntent.TitleChooser titleChooser =
                        ActionsSuggestionsHelper.createTitleChooser(actionType);
                LabeledIntent.Result result = labeledIntents.get(0).resolve(mContext, titleChooser);
                if (result != null) {
                    remoteAction = result.remoteAction;
                    ExtrasUtils.putActionIntent(extras, result.resolvedIntent);
                }
            Bundle extras = new Bundle();
            if (labeledIntentResult != null) {
                remoteAction = labeledIntentResult.remoteAction;
                ExtrasUtils.putActionIntent(extras, labeledIntentResult.resolvedIntent);
            }
            conversationActions.add(
                    new ConversationAction.Builder(actionType)
@@ -620,7 +621,7 @@ public final class TextClassifierImpl implements TextClassifier {
        builder.setForeignLanguageExtra(foreignLanguageBundle);

        boolean isPrimaryAction = true;
        List<LabeledIntent> labeledIntents = mIntentFactory.create(
        List<LabeledIntent> labeledIntents = mClassificationIntentFactory.create(
                mContext,
                classifiedText,
                foreignLanguageBundle != null,
+3 −2
Original line number Diff line number Diff line
@@ -13,11 +13,12 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.view.textclassifier;
package android.view.textclassifier.intent;

import android.annotation.Nullable;
import android.content.Context;
import android.content.Intent;
import android.view.textclassifier.TextClassifier;

import com.google.android.textclassifier.AnnotatorModel;

@@ -27,7 +28,7 @@ import java.util.List;
/**
 * @hide
 */
public interface IntentFactory {
public interface ClassificationIntentFactory {

    /**
     * Return a list of LabeledIntent from the classification result.
+3 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.view.textclassifier;
package android.view.textclassifier.intent;

import android.annotation.Nullable;
import android.app.PendingIntent;
@@ -25,6 +25,8 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Icon;
import android.text.TextUtils;
import android.view.textclassifier.Log;
import android.view.textclassifier.TextClassification;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.Preconditions;
Loading