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 com.android.textclassifier.common.logging;
18 
19 import androidx.annotation.NonNull;
20 import com.google.common.base.Preconditions;
21 import java.util.Locale;
22 import javax.annotation.Nullable;
23 
24 /** A representation of the context in which text classification would be performed. */
25 public final class TextClassificationContext {
26 
27   private final String packageName;
28   private final String widgetType;
29   @Nullable private final String widgetVersion;
30 
TextClassificationContext( String packageName, String widgetType, @Nullable String widgetVersion)31   private TextClassificationContext(
32       String packageName, String widgetType, @Nullable String widgetVersion) {
33     this.packageName = Preconditions.checkNotNull(packageName);
34     this.widgetType = Preconditions.checkNotNull(widgetType);
35     this.widgetVersion = widgetVersion;
36   }
37 
38   /** Returns the package name for the calling package. */
getPackageName()39   public String getPackageName() {
40     return packageName;
41   }
42 
43   /** Returns the widget type for this classification context. */
getWidgetType()44   public String getWidgetType() {
45     return widgetType;
46   }
47 
48   /**
49    * Returns a custom version string for the widget type.
50    *
51    * @see #getWidgetType()
52    */
53   @Nullable
getWidgetVersion()54   public String getWidgetVersion() {
55     return widgetVersion;
56   }
57 
58   @Override
toString()59   public String toString() {
60     return String.format(
61         Locale.US,
62         "TextClassificationContext{" + "packageName=%s, widgetType=%s, widgetVersion=%s}",
63         packageName,
64         widgetType,
65         widgetVersion);
66   }
67 
68   /** A builder for building a TextClassification context. */
69   public static final class Builder {
70 
71     private final String packageName;
72     private final String widgetType;
73 
74     @Nullable private String widgetVersion;
75 
76     /**
77      * Initializes a new builder for text classification context objects.
78      *
79      * @param packageName the name of the calling package
80      * @param widgetType the type of widget e.g. {@link
81      *     android.view.textclassifier.TextClassifier#WIDGET_TYPE_TEXTVIEW}
82      * @return this builder
83      */
Builder(String packageName, String widgetType)84     public Builder(String packageName, String widgetType) {
85       this.packageName = Preconditions.checkNotNull(packageName);
86       this.widgetType = Preconditions.checkNotNull(widgetType);
87     }
88 
89     /**
90      * Sets an optional custom version string for the widget type.
91      *
92      * @return this builder
93      */
setWidgetVersion(@ullable String widgetVersion)94     public Builder setWidgetVersion(@Nullable String widgetVersion) {
95       this.widgetVersion = widgetVersion;
96       return this;
97     }
98 
99     /**
100      * Builds the text classification context object.
101      *
102      * @return the built TextClassificationContext object
103      */
104     @NonNull
build()105     public TextClassificationContext build() {
106       return new TextClassificationContext(packageName, this.widgetType, widgetVersion);
107     }
108   }
109 }
110