blob: 9e2b6427eaeac80cf22afae07794dbafe6682092 [file] [log] [blame]
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +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 */
16
17package android.view.textclassifier;
18
19import android.annotation.Nullable;
20import android.app.RemoteAction;
21import android.content.Intent;
22import android.os.Bundle;
23
24import java.util.ArrayList;
25
26/**
27 * Utility class for inserting and retrieving data in TextClassifier request/response extras.
28 * @hide
29 */
Tony Mak72e17972019-03-16 10:28:42 +000030// TODO: Make this a TestApi for CTS testing.
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000031public final class ExtrasUtils {
32
Tony Makb6afd3c2019-04-05 15:45:18 +010033 // Keys for response objects.
Tony Makc12035e2019-02-26 17:45:34 +000034 private static final String ACTION_INTENT = "action-intent";
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000035 private static final String ACTIONS_INTENTS = "actions-intents";
36 private static final String FOREIGN_LANGUAGE = "foreign-language";
37 private static final String ENTITY_TYPE = "entity-type";
38 private static final String SCORE = "score";
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000039 private static final String MODEL_NAME = "model-name";
40
Tony Mak293bdf32020-02-18 11:33:43 +000041 private ExtrasUtils() {
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000042 }
43
44 /**
45 * Returns foreign language detection information contained in the TextClassification object.
46 * responses.
47 */
48 @Nullable
Tony Mak72e17972019-03-16 10:28:42 +000049 public static Bundle getForeignLanguageExtra(@Nullable TextClassification classification) {
50 if (classification == null) {
51 return null;
52 }
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000053 return classification.getExtras().getBundle(FOREIGN_LANGUAGE);
54 }
55
56 /**
Tony Makc12035e2019-02-26 17:45:34 +000057 * Returns {@code actionIntent} information contained in a TextClassifier response object.
58 */
59 @Nullable
60 public static Intent getActionIntent(Bundle container) {
61 return container.getParcelable(ACTION_INTENT);
62 }
63
64 /**
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000065 * Returns {@code actionIntents} information contained in the TextClassification object.
66 */
67 @Nullable
Tony Mak72e17972019-03-16 10:28:42 +000068 public static ArrayList<Intent> getActionsIntents(@Nullable TextClassification classification) {
69 if (classification == null) {
70 return null;
71 }
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000072 return classification.getExtras().getParcelableArrayList(ACTIONS_INTENTS);
73 }
74
75 /**
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +000076 * Returns the first action found in the {@code classification} object with an intent
77 * action string, {@code intentAction}.
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000078 */
79 @Nullable
Tony Mak293bdf32020-02-18 11:33:43 +000080 private static RemoteAction findAction(
Tony Mak72e17972019-03-16 10:28:42 +000081 @Nullable TextClassification classification, @Nullable String intentAction) {
82 if (classification == null || intentAction == null) {
83 return null;
84 }
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000085 final ArrayList<Intent> actionIntents = getActionsIntents(classification);
86 if (actionIntents != null) {
87 final int size = actionIntents.size();
88 for (int i = 0; i < size; i++) {
Abodunrinwa Toki25f7fdc2019-02-19 23:42:30 +000089 final Intent intent = actionIntents.get(i);
90 if (intent != null && intentAction.equals(intent.getAction())) {
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +000091 return classification.getActions().get(i);
92 }
93 }
94 }
95 return null;
96 }
97
98 /**
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +000099 * Returns the first "translate" action found in the {@code classification} object.
100 */
101 @Nullable
Tony Mak72e17972019-03-16 10:28:42 +0000102 public static RemoteAction findTranslateAction(@Nullable TextClassification classification) {
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +0000103 return findAction(classification, Intent.ACTION_TRANSLATE);
104 }
105
106 /**
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +0000107 * Returns the entity type contained in the {@code extra}.
108 */
109 @Nullable
Tony Mak72e17972019-03-16 10:28:42 +0000110 public static String getEntityType(@Nullable Bundle extra) {
111 if (extra == null) {
112 return null;
113 }
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +0000114 return extra.getString(ENTITY_TYPE);
115 }
116
117 /**
118 * Returns the score contained in the {@code extra}.
119 */
120 @Nullable
121 public static float getScore(Bundle extra) {
Tony Mak72e17972019-03-16 10:28:42 +0000122 final int defaultValue = -1;
123 if (extra == null) {
124 return defaultValue;
125 }
126 return extra.getFloat(SCORE, defaultValue);
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +0000127 }
128
129 /**
130 * Returns the model name contained in the {@code extra}.
131 */
132 @Nullable
Tony Mak72e17972019-03-16 10:28:42 +0000133 public static String getModelName(@Nullable Bundle extra) {
134 if (extra == null) {
135 return null;
136 }
Abodunrinwa Toki520b2f82019-01-27 07:48:02 +0000137 return extra.getString(MODEL_NAME);
138 }
Tony Mak293bdf32020-02-18 11:33:43 +0000139}