Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +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 | */ |
| 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 | */ |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 30 | // TODO: Make this a TestApi for CTS testing. |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 31 | public final class ExtrasUtils { |
| 32 | |
Tony Mak | b6afd3c | 2019-04-05 15:45:18 +0100 | [diff] [blame] | 33 | // Keys for response objects. |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 34 | private static final String ACTION_INTENT = "action-intent"; |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 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"; |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 39 | private static final String MODEL_NAME = "model-name"; |
| 40 | |
Tony Mak | 293bdf3 | 2020-02-18 11:33:43 +0000 | [diff] [blame^] | 41 | private ExtrasUtils() { |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns foreign language detection information contained in the TextClassification object. |
| 46 | * responses. |
| 47 | */ |
| 48 | @Nullable |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 49 | public static Bundle getForeignLanguageExtra(@Nullable TextClassification classification) { |
| 50 | if (classification == null) { |
| 51 | return null; |
| 52 | } |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 53 | return classification.getExtras().getBundle(FOREIGN_LANGUAGE); |
| 54 | } |
| 55 | |
| 56 | /** |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 57 | * 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 Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 65 | * Returns {@code actionIntents} information contained in the TextClassification object. |
| 66 | */ |
| 67 | @Nullable |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 68 | public static ArrayList<Intent> getActionsIntents(@Nullable TextClassification classification) { |
| 69 | if (classification == null) { |
| 70 | return null; |
| 71 | } |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 72 | return classification.getExtras().getParcelableArrayList(ACTIONS_INTENTS); |
| 73 | } |
| 74 | |
| 75 | /** |
Abodunrinwa Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 76 | * Returns the first action found in the {@code classification} object with an intent |
| 77 | * action string, {@code intentAction}. |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 78 | */ |
| 79 | @Nullable |
Tony Mak | 293bdf3 | 2020-02-18 11:33:43 +0000 | [diff] [blame^] | 80 | private static RemoteAction findAction( |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 81 | @Nullable TextClassification classification, @Nullable String intentAction) { |
| 82 | if (classification == null || intentAction == null) { |
| 83 | return null; |
| 84 | } |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 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++) { |
Abodunrinwa Toki | 25f7fdc | 2019-02-19 23:42:30 +0000 | [diff] [blame] | 89 | final Intent intent = actionIntents.get(i); |
| 90 | if (intent != null && intentAction.equals(intent.getAction())) { |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 91 | return classification.getActions().get(i); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | /** |
Abodunrinwa Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 99 | * Returns the first "translate" action found in the {@code classification} object. |
| 100 | */ |
| 101 | @Nullable |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 102 | public static RemoteAction findTranslateAction(@Nullable TextClassification classification) { |
Abodunrinwa Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 103 | return findAction(classification, Intent.ACTION_TRANSLATE); |
| 104 | } |
| 105 | |
| 106 | /** |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 107 | * Returns the entity type contained in the {@code extra}. |
| 108 | */ |
| 109 | @Nullable |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 110 | public static String getEntityType(@Nullable Bundle extra) { |
| 111 | if (extra == null) { |
| 112 | return null; |
| 113 | } |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 114 | 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 Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 122 | final int defaultValue = -1; |
| 123 | if (extra == null) { |
| 124 | return defaultValue; |
| 125 | } |
| 126 | return extra.getFloat(SCORE, defaultValue); |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the model name contained in the {@code extra}. |
| 131 | */ |
| 132 | @Nullable |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 133 | public static String getModelName(@Nullable Bundle extra) { |
| 134 | if (extra == null) { |
| 135 | return null; |
| 136 | } |
Abodunrinwa Toki | 520b2f8 | 2019-01-27 07:48:02 +0000 | [diff] [blame] | 137 | return extra.getString(MODEL_NAME); |
| 138 | } |
Tony Mak | 293bdf3 | 2020-02-18 11:33:43 +0000 | [diff] [blame^] | 139 | } |