1 /*
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  */
16 
17 package android.view.textclassifier;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.view.textclassifier.TextClassifier.WidgetType;
24 
25 import java.util.Locale;
26 import java.util.Objects;
27 
28 /**
29  * A representation of the context in which text classification would be performed.
30  * @see TextClassificationManager#createTextClassificationSession(TextClassificationContext)
31  */
32 public final class TextClassificationContext implements Parcelable {
33 
34     private String mPackageName;
35     private final String mWidgetType;
36     @Nullable private final String mWidgetVersion;
37     private SystemTextClassifierMetadata mSystemTcMetadata;
38 
TextClassificationContext( String packageName, String widgetType, String widgetVersion)39     private TextClassificationContext(
40             String packageName,
41             String widgetType,
42             String widgetVersion) {
43         mPackageName = Objects.requireNonNull(packageName);
44         mWidgetType = Objects.requireNonNull(widgetType);
45         mWidgetVersion = widgetVersion;
46     }
47 
48     /**
49      * Returns the package name of the app that this context originated in.
50      */
51     @NonNull
getPackageName()52     public String getPackageName() {
53         return mPackageName;
54     }
55 
56     /**
57      * Sets the information about the {@link SystemTextClassifier} that sent this request.
58      *
59      * @hide
60      */
setSystemTextClassifierMetadata(@ullable SystemTextClassifierMetadata systemTcMetadata)61     void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcMetadata) {
62         mSystemTcMetadata = systemTcMetadata;
63     }
64 
65     /**
66      * Returns the information about the {@link SystemTextClassifier} that sent this request.
67      *
68      * @hide
69      */
70     @Nullable
getSystemTextClassifierMetadata()71     public SystemTextClassifierMetadata getSystemTextClassifierMetadata() {
72         return mSystemTcMetadata;
73     }
74 
75     /**
76      * Returns the widget type for this classification context.
77      */
78     @NonNull
79     @WidgetType
getWidgetType()80     public String getWidgetType() {
81         return mWidgetType;
82     }
83 
84     /**
85      * Returns a custom version string for the widget type.
86      *
87      * @see #getWidgetType()
88      */
89     @Nullable
getWidgetVersion()90     public String getWidgetVersion() {
91         return mWidgetVersion;
92     }
93 
94     @Override
toString()95     public String toString() {
96         return String.format(Locale.US, "TextClassificationContext{"
97                 + "packageName=%s, widgetType=%s, widgetVersion=%s, systemTcMetadata=%s}",
98                 mPackageName, mWidgetType, mWidgetVersion, mSystemTcMetadata);
99     }
100 
101     /**
102      * A builder for building a TextClassification context.
103      */
104     public static final class Builder {
105 
106         private final String mPackageName;
107         private final String mWidgetType;
108 
109         @Nullable private String mWidgetVersion;
110 
111         /**
112          * Initializes a new builder for text classification context objects.
113          *
114          * @param packageName the name of the calling package
115          * @param widgetType the type of widget e.g. {@link TextClassifier#WIDGET_TYPE_TEXTVIEW}
116          *
117          * @return this builder
118          */
Builder(@onNull String packageName, @NonNull @WidgetType String widgetType)119         public Builder(@NonNull String packageName, @NonNull @WidgetType String widgetType) {
120             mPackageName = Objects.requireNonNull(packageName);
121             mWidgetType = Objects.requireNonNull(widgetType);
122         }
123 
124         /**
125          * Sets an optional custom version string for the widget type.
126          *
127          * @return this builder
128          */
setWidgetVersion(@ullable String widgetVersion)129         public Builder setWidgetVersion(@Nullable String widgetVersion) {
130             mWidgetVersion = widgetVersion;
131             return this;
132         }
133 
134         /**
135          * Builds the text classification context object.
136          *
137          * @return the built TextClassificationContext object
138          */
139         @NonNull
build()140         public TextClassificationContext build() {
141             return new TextClassificationContext(mPackageName, mWidgetType, mWidgetVersion);
142         }
143     }
144 
145     @Override
describeContents()146     public int describeContents() {
147         return 0;
148     }
149 
150     @Override
writeToParcel(Parcel parcel, int flags)151     public void writeToParcel(Parcel parcel, int flags) {
152         parcel.writeString(mPackageName);
153         parcel.writeString(mWidgetType);
154         parcel.writeString(mWidgetVersion);
155         parcel.writeParcelable(mSystemTcMetadata, flags);
156     }
157 
TextClassificationContext(Parcel in)158     private TextClassificationContext(Parcel in) {
159         mPackageName = in.readString();
160         mWidgetType = in.readString();
161         mWidgetVersion = in.readString();
162         mSystemTcMetadata = in.readParcelable(null, android.view.textclassifier.SystemTextClassifierMetadata.class);
163     }
164 
165     public static final @android.annotation.NonNull Parcelable.Creator<TextClassificationContext> CREATOR =
166             new Parcelable.Creator<TextClassificationContext>() {
167                 @Override
168                 public TextClassificationContext createFromParcel(Parcel parcel) {
169                     return new TextClassificationContext(parcel);
170                 }
171 
172                 @Override
173                 public TextClassificationContext[] newArray(int size) {
174                     return new TextClassificationContext[size];
175                 }
176             };
177 }
178