| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public final class ExtrasUtils { |
| 32 |
|
| 33 |
|
| 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 |
|
| 41 |
private ExtrasUtils() { |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@Nullable |
| 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 |
|
| 58 |
|
| 59 |
@Nullable |
| 60 |
public static Intent getActionIntent(Bundle container) { |
| 61 |
return container.getParcelable(ACTION_INTENT); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
@Nullable |
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
@Nullable |
| 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 |
|
| 100 |
|
| 101 |
@Nullable |
| 102 |
public static RemoteAction findTranslateAction(@Nullable TextClassification classification) { |
| 103 |
return findAction(classification, Intent.ACTION_TRANSLATE); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
@Nullable |
| 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 |
|
| 119 |
|
| 120 |
@Nullable |
| 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 |
|
| 131 |
|
| 132 |
@Nullable |
| 133 |
public static String getModelName(@Nullable Bundle extra) { |
| 134 |
if (extra == null) { |
| 135 |
return null; |
| 136 |
} |
| 137 |
return extra.getString(MODEL_NAME); |
| 138 |
} |
| 139 |
} |