blob: ae6a645c90a1d548db517654f4290293a1ca76b1 [file] [log] [blame]
Tony Makae85aae2019-01-09 15:59:56 +00001/*
2 * Copyright (C) 2018 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 */
16package android.view.textclassifier;
17
18import static java.lang.annotation.RetentionPolicy.SOURCE;
19
20import android.annotation.FloatRange;
21import android.annotation.NonNull;
22import android.annotation.Nullable;
23import android.annotation.StringDef;
24import android.app.RemoteAction;
25import android.os.Bundle;
26import android.os.Parcel;
27import android.os.Parcelable;
28
29import com.android.internal.util.Preconditions;
30
31import java.lang.annotation.Retention;
32
33/** Represents the action suggested by a {@link TextClassifier} on a given conversation. */
34public final class ConversationAction implements Parcelable {
35
36 /** @hide */
37 @Retention(SOURCE)
38 @StringDef(
39 value = {
40 TYPE_VIEW_CALENDAR,
41 TYPE_VIEW_MAP,
42 TYPE_TRACK_FLIGHT,
43 TYPE_OPEN_URL,
44 TYPE_SEND_SMS,
45 TYPE_CALL_PHONE,
46 TYPE_SEND_EMAIL,
47 TYPE_TEXT_REPLY,
48 TYPE_CREATE_REMINDER,
49 TYPE_SHARE_LOCATION
50 },
51 prefix = "TYPE_")
52 public @interface ActionType {}
53
54 /**
55 * Indicates an action to view a calendar at a specified time.
56 */
57 public static final String TYPE_VIEW_CALENDAR = "view_calendar";
58 /**
59 * Indicates an action to view the map at a specified location.
60 */
61 public static final String TYPE_VIEW_MAP = "view_map";
62 /**
63 * Indicates an action to track a flight.
64 */
65 public static final String TYPE_TRACK_FLIGHT = "track_flight";
66 /**
67 * Indicates an action to open an URL.
68 */
69 public static final String TYPE_OPEN_URL = "open_url";
70 /**
71 * Indicates an action to send a SMS.
72 */
73 public static final String TYPE_SEND_SMS = "send_sms";
74 /**
75 * Indicates an action to call a phone number.
76 */
77 public static final String TYPE_CALL_PHONE = "call_phone";
78 /**
79 * Indicates an action to send an email.
80 */
81 public static final String TYPE_SEND_EMAIL = "send_email";
82 /**
83 * Indicates an action to reply with a text message.
84 */
85 public static final String TYPE_TEXT_REPLY = "text_reply";
86 /**
87 * Indicates an action to create a reminder.
88 */
89 public static final String TYPE_CREATE_REMINDER = "create_reminder";
90 /**
91 * Indicates an action to reply with a location.
92 */
93 public static final String TYPE_SHARE_LOCATION = "share_location";
94
Tony Maka396be42019-02-07 14:37:22 +000095 /** @hide **/
96 public static final String TYPE_ADD_CONTACT = "add_contact";
97
Tony Makae85aae2019-01-09 15:59:56 +000098 public static final Creator<ConversationAction> CREATOR =
99 new Creator<ConversationAction>() {
100 @Override
101 public ConversationAction createFromParcel(Parcel in) {
102 return new ConversationAction(in);
103 }
104
105 @Override
106 public ConversationAction[] newArray(int size) {
107 return new ConversationAction[size];
108 }
109 };
110
111 @NonNull
112 @ActionType
113 private final String mType;
114 @NonNull
115 private final CharSequence mTextReply;
116 @Nullable
117 private final RemoteAction mAction;
118
119 @FloatRange(from = 0, to = 1)
120 private final float mScore;
121
122 @NonNull
123 private final Bundle mExtras;
124
125 private ConversationAction(
126 @NonNull String type,
127 @Nullable RemoteAction action,
128 @Nullable CharSequence textReply,
129 float score,
130 @NonNull Bundle extras) {
131 mType = Preconditions.checkNotNull(type);
132 mAction = action;
133 mTextReply = textReply;
134 mScore = score;
135 mExtras = Preconditions.checkNotNull(extras);
136 }
137
138 private ConversationAction(Parcel in) {
139 mType = in.readString();
140 mAction = in.readParcelable(null);
141 mTextReply = in.readCharSequence();
142 mScore = in.readFloat();
143 mExtras = in.readBundle();
144 }
145
146 @Override
147 public void writeToParcel(Parcel parcel, int flags) {
148 parcel.writeString(mType);
149 parcel.writeParcelable(mAction, flags);
150 parcel.writeCharSequence(mTextReply);
151 parcel.writeFloat(mScore);
152 parcel.writeBundle(mExtras);
153 }
154
155 @Override
156 public int describeContents() {
157 return 0;
158 }
159
160 /** Returns the type of this action, for example, {@link #TYPE_VIEW_CALENDAR}. */
161 @NonNull
162 @ActionType
163 public String getType() {
164 return mType;
165 }
166
167 /**
168 * Returns a RemoteAction object, which contains the icon, label and a PendingIntent, for
169 * the specified action type.
170 */
171 @Nullable
172 public RemoteAction getAction() {
173 return mAction;
174 }
175
176 /**
177 * Returns the confidence score for the specified action. The value ranges from 0 (low
178 * confidence) to 1 (high confidence).
179 */
180 @FloatRange(from = 0, to = 1)
181 public float getConfidenceScore() {
182 return mScore;
183 }
184
185 /**
186 * Returns the text reply that could be sent as a reply to the given conversation.
187 * <p>
188 * This is only available when the type of the action is {@link #TYPE_TEXT_REPLY}.
189 */
190 @Nullable
191 public CharSequence getTextReply() {
192 return mTextReply;
193 }
194
195 /**
196 * Returns the extended data related to this conversation action.
197 *
198 * <p><b>NOTE: </b>Each call to this method returns a new bundle copy so clients should
199 * prefer to hold a reference to the returned bundle rather than frequently calling this
200 * method.
201 */
202 @NonNull
203 public Bundle getExtras() {
204 return mExtras.deepCopy();
205 }
206
207 /** Builder class to construct {@link ConversationAction}. */
208 public static final class Builder {
209 @Nullable
210 @ActionType
211 private String mType;
212 @Nullable
213 private RemoteAction mAction;
214 @Nullable
215 private CharSequence mTextReply;
216 private float mScore;
217 @Nullable
218 private Bundle mExtras;
219
220 public Builder(@NonNull @ActionType String actionType) {
221 mType = Preconditions.checkNotNull(actionType);
222 }
223
224 /**
225 * Sets an action that may be performed on the given conversation.
226 */
227 @NonNull
228 public Builder setAction(@Nullable RemoteAction action) {
229 mAction = action;
230 return this;
231 }
232
233 /**
234 * Sets a text reply that may be performed on the given conversation.
235 */
236 @NonNull
237 public Builder setTextReply(@Nullable CharSequence textReply) {
238 mTextReply = textReply;
239 return this;
240 }
241
242 /** Sets the confident score. */
243 @NonNull
244 public Builder setConfidenceScore(@FloatRange(from = 0, to = 1) float score) {
245 mScore = score;
246 return this;
247 }
248
249 /**
250 * Sets the extended data for the conversation action object.
251 */
252 @NonNull
253 public Builder setExtras(@Nullable Bundle extras) {
254 mExtras = extras;
255 return this;
256 }
257
258 /** Builds the {@link ConversationAction} object. */
259 @NonNull
260 public ConversationAction build() {
261 return new ConversationAction(
262 mType,
263 mAction,
264 mTextReply,
265 mScore,
266 mExtras == null ? Bundle.EMPTY : mExtras.deepCopy());
267 }
268 }
269}