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