Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 1 | /* |
| 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 Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 16 | package android.view.textclassifier.intent; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 21 | import android.view.textclassifier.Log; |
| 22 | import android.view.textclassifier.TextClassifier; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 23 | |
| 24 | import com.android.internal.annotations.VisibleForTesting; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 25 | import com.android.internal.util.Preconditions; |
| 26 | |
| 27 | import com.google.android.textclassifier.AnnotatorModel; |
| 28 | import com.google.android.textclassifier.RemoteActionTemplate; |
| 29 | |
| 30 | import java.time.Instant; |
| 31 | import java.util.Collections; |
| 32 | import java.util.List; |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 33 | import java.util.Objects; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Creates intents based on {@link RemoteActionTemplate} objects for a ClassificationResult. |
| 37 | * |
| 38 | * @hide |
| 39 | */ |
| 40 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 41 | public final class TemplateClassificationIntentFactory implements ClassificationIntentFactory { |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 42 | private static final String TAG = TextClassifier.DEFAULT_LOG_TAG; |
| 43 | private final TemplateIntentFactory mTemplateIntentFactory; |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 44 | private final ClassificationIntentFactory mFallback; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 45 | |
| 46 | public TemplateClassificationIntentFactory(TemplateIntentFactory templateIntentFactory, |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 47 | ClassificationIntentFactory fallback) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 48 | mTemplateIntentFactory = Objects.requireNonNull(templateIntentFactory); |
| 49 | mFallback = Objects.requireNonNull(fallback); |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 53 | * Returns a list of {@link LabeledIntent} |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 54 | * that are constructed from the classification result. |
| 55 | */ |
| 56 | @NonNull |
| 57 | @Override |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 58 | public List<LabeledIntent> create( |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 59 | 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 Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 68 | if (remoteActionTemplates == null) { |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 69 | // RemoteActionTemplate is missing, fallback. |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 70 | Log.w(TAG, "RemoteActionTemplate is missing, fallback to" |
| 71 | + " LegacyClassificationIntentFactory."); |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 72 | return mFallback.create(context, text, foreignText, referenceTime, classification); |
| 73 | } |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 74 | final List<LabeledIntent> labeledIntents = |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 75 | mTemplateIntentFactory.create(remoteActionTemplates); |
| 76 | if (foreignText) { |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 77 | ClassificationIntentFactory.insertTranslateAction(labeledIntents, context, text.trim()); |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 78 | } |
| 79 | return labeledIntents; |
| 80 | } |
| 81 | } |