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 */ 16 17 package android.view.textclassifier; 18 19 import android.annotation.Nullable; 20 import android.app.RemoteAction; 21 import android.content.Intent; 22 import android.os.Bundle; 23 24 import java.util.ArrayList; 25 26 /** 27 * Utility class for inserting and retrieving data in TextClassifier request/response extras. 28 * @hide 29 */ 30 // TODO: Make this a TestApi for CTS testing. 31 public final class ExtrasUtils { 32 33 // Keys for response objects. 34 private static final String ACTION_INTENT = "action-intent"; 35 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"; 39 private static final String MODEL_NAME = "model-name"; 40 ExtrasUtils()41 private ExtrasUtils() { 42 } 43 44 /** 45 * Returns foreign language detection information contained in the TextClassification object. 46 * responses. 47 */ 48 @Nullable getForeignLanguageExtra(@ullable TextClassification classification)49 public static Bundle getForeignLanguageExtra(@Nullable TextClassification classification) { 50 if (classification == null) { 51 return null; 52 } 53 return classification.getExtras().getBundle(FOREIGN_LANGUAGE); 54 } 55 56 /** 57 * Returns {@code actionIntent} information contained in a TextClassifier response object. 58 */ 59 @Nullable getActionIntent(Bundle container)60 public static Intent getActionIntent(Bundle container) { 61 return container.getParcelable(ACTION_INTENT); 62 } 63 64 /** 65 * Returns {@code actionIntents} information contained in the TextClassification object. 66 */ 67 @Nullable getActionsIntents(@ullable TextClassification classification)68 public static ArrayList<Intent> getActionsIntents(@Nullable TextClassification classification) { 69 if (classification == null) { 70 return null; 71 } 72 return classification.getExtras().getParcelableArrayList(ACTIONS_INTENTS); 73 } 74 75 /** 76 * Returns the first action found in the {@code classification} object with an intent 77 * action string, {@code intentAction}. 78 */ 79 @Nullable findAction( @ullable TextClassification classification, @Nullable String intentAction)80 private static RemoteAction findAction( 81 @Nullable TextClassification classification, @Nullable String intentAction) { 82 if (classification == null || intentAction == null) { 83 return null; 84 } 85 final ArrayList<Intent> actionIntents = getActionsIntents(classification); 86 if (actionIntents != null) { 87 final int size = actionIntents.size(); 88 for (int i = 0; i < size; i++) { 89 final Intent intent = actionIntents.get(i); 90 if (intent != null && intentAction.equals(intent.getAction())) { 91 return classification.getActions().get(i); 92 } 93 } 94 } 95 return null; 96 } 97 98 /** 99 * Returns the first "translate" action found in the {@code classification} object. 100 */ 101 @Nullable findTranslateAction(@ullable TextClassification classification)102 public static RemoteAction findTranslateAction(@Nullable TextClassification classification) { 103 return findAction(classification, Intent.ACTION_TRANSLATE); 104 } 105 106 /** 107 * Returns the entity type contained in the {@code extra}. 108 */ 109 @Nullable getEntityType(@ullable Bundle extra)110 public static String getEntityType(@Nullable Bundle extra) { 111 if (extra == null) { 112 return null; 113 } 114 return extra.getString(ENTITY_TYPE); 115 } 116 117 /** 118 * Returns the score contained in the {@code extra}. 119 */ 120 @Nullable getScore(Bundle extra)121 public static float getScore(Bundle extra) { 122 final int defaultValue = -1; 123 if (extra == null) { 124 return defaultValue; 125 } 126 return extra.getFloat(SCORE, defaultValue); 127 } 128 129 /** 130 * Returns the model name contained in the {@code extra}. 131 */ 132 @Nullable getModelName(@ullable Bundle extra)133 public static String getModelName(@Nullable Bundle extra) { 134 if (extra == null) { 135 return null; 136 } 137 return extra.getString(MODEL_NAME); 138 } 139 }