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