blob: aef4bd6c7351095f49c21f46b4e59facd626fa77 [file] [log] [blame]
Tony Makae33c3b2019-01-31 14:29:19 +00001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Tony Mak8ab9b182019-03-01 16:44:17 +000016package android.view.textclassifier.intent;
Tony Makae33c3b2019-01-31 14:29:19 +000017
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.content.Context;
Tony Mak8ab9b182019-03-01 16:44:17 +000021import android.view.textclassifier.Log;
22import android.view.textclassifier.TextClassifier;
Tony Makae33c3b2019-01-31 14:29:19 +000023
24import com.android.internal.annotations.VisibleForTesting;
Tony Makae33c3b2019-01-31 14:29:19 +000025import com.android.internal.util.Preconditions;
26
27import com.google.android.textclassifier.AnnotatorModel;
28import com.google.android.textclassifier.RemoteActionTemplate;
29
30import java.time.Instant;
31import java.util.Collections;
32import java.util.List;
Daulet Zhanguzine1559472019-12-18 14:17:56 +000033import java.util.Objects;
Tony Makae33c3b2019-01-31 14:29:19 +000034
35/**
36 * Creates intents based on {@link RemoteActionTemplate} objects for a ClassificationResult.
37 *
38 * @hide
39 */
40@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
Tony Mak8ab9b182019-03-01 16:44:17 +000041public final class TemplateClassificationIntentFactory implements ClassificationIntentFactory {
Tony Makae33c3b2019-01-31 14:29:19 +000042 private static final String TAG = TextClassifier.DEFAULT_LOG_TAG;
43 private final TemplateIntentFactory mTemplateIntentFactory;
Tony Mak8ab9b182019-03-01 16:44:17 +000044 private final ClassificationIntentFactory mFallback;
Tony Makae33c3b2019-01-31 14:29:19 +000045
46 public TemplateClassificationIntentFactory(TemplateIntentFactory templateIntentFactory,
Tony Mak8ab9b182019-03-01 16:44:17 +000047 ClassificationIntentFactory fallback) {
Daulet Zhanguzine1559472019-12-18 14:17:56 +000048 mTemplateIntentFactory = Objects.requireNonNull(templateIntentFactory);
49 mFallback = Objects.requireNonNull(fallback);
Tony Makae33c3b2019-01-31 14:29:19 +000050 }
51
52 /**
Tony Mak8ab9b182019-03-01 16:44:17 +000053 * Returns a list of {@link LabeledIntent}
Tony Makae33c3b2019-01-31 14:29:19 +000054 * that are constructed from the classification result.
55 */
56 @NonNull
57 @Override
Tony Makac9b4d82019-02-15 13:57:38 +000058 public List<LabeledIntent> create(
Tony Makae33c3b2019-01-31 14:29:19 +000059 Context context,
60 String text,
61 boolean foreignText,
62 @Nullable Instant referenceTime,
63 @Nullable AnnotatorModel.ClassificationResult classification) {
64 if (classification == null) {
65 return Collections.emptyList();
66 }
67 RemoteActionTemplate[] remoteActionTemplates = classification.getRemoteActionTemplates();
Tony Mak8ab9b182019-03-01 16:44:17 +000068 if (remoteActionTemplates == null) {
Tony Makae33c3b2019-01-31 14:29:19 +000069 // RemoteActionTemplate is missing, fallback.
Tony Mak8ab9b182019-03-01 16:44:17 +000070 Log.w(TAG, "RemoteActionTemplate is missing, fallback to"
71 + " LegacyClassificationIntentFactory.");
Tony Makae33c3b2019-01-31 14:29:19 +000072 return mFallback.create(context, text, foreignText, referenceTime, classification);
73 }
Tony Makac9b4d82019-02-15 13:57:38 +000074 final List<LabeledIntent> labeledIntents =
Tony Makae33c3b2019-01-31 14:29:19 +000075 mTemplateIntentFactory.create(remoteActionTemplates);
76 if (foreignText) {
Tony Mak8ab9b182019-03-01 16:44:17 +000077 ClassificationIntentFactory.insertTranslateAction(labeledIntents, context, text.trim());
Tony Makae33c3b2019-01-31 14:29:19 +000078 }
79 return labeledIntents;
80 }
81}