Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 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; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 23 | import android.view.textclassifier.TextClassifier.WidgetType; |
| 24 | |
| 25 | import com.android.internal.util.Preconditions; |
| 26 | |
| 27 | import java.util.Locale; |
| 28 | |
| 29 | /** |
| 30 | * A representation of the context in which text classification would be performed. |
| 31 | * @see TextClassificationManager#createTextClassificationSession(TextClassificationContext) |
| 32 | */ |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 33 | public final class TextClassificationContext implements Parcelable { |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 34 | |
| 35 | private final String mPackageName; |
| 36 | private final String mWidgetType; |
| 37 | @Nullable private final String mWidgetVersion; |
| 38 | |
| 39 | private TextClassificationContext( |
| 40 | String packageName, |
| 41 | String widgetType, |
| 42 | String widgetVersion) { |
| 43 | mPackageName = Preconditions.checkNotNull(packageName); |
| 44 | mWidgetType = Preconditions.checkNotNull(widgetType); |
| 45 | mWidgetVersion = widgetVersion; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns the package name for the calling package. |
| 50 | */ |
| 51 | @NonNull |
| 52 | public String getPackageName() { |
| 53 | return mPackageName; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the widget type for this classification context. |
| 58 | */ |
| 59 | @NonNull |
| 60 | @WidgetType |
| 61 | public String getWidgetType() { |
| 62 | return mWidgetType; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns a custom version string for the widget type. |
| 67 | * |
| 68 | * @see #getWidgetType() |
| 69 | */ |
| 70 | @Nullable |
| 71 | public String getWidgetVersion() { |
| 72 | return mWidgetVersion; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String toString() { |
| 77 | return String.format(Locale.US, "TextClassificationContext{" |
| 78 | + "packageName=%s, widgetType=%s, widgetVersion=%s}", |
| 79 | mPackageName, mWidgetType, mWidgetVersion); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * A builder for building a TextClassification context. |
| 84 | */ |
| 85 | public static final class Builder { |
| 86 | |
| 87 | private final String mPackageName; |
| 88 | private final String mWidgetType; |
| 89 | |
| 90 | @Nullable private String mWidgetVersion; |
| 91 | |
| 92 | /** |
| 93 | * Initializes a new builder for text classification context objects. |
| 94 | * |
| 95 | * @param packageName the name of the calling package |
| 96 | * @param widgetType the type of widget e.g. {@link TextClassifier#WIDGET_TYPE_TEXTVIEW} |
| 97 | * |
| 98 | * @return this builder |
| 99 | */ |
| 100 | public Builder(@NonNull String packageName, @NonNull @WidgetType String widgetType) { |
| 101 | mPackageName = Preconditions.checkNotNull(packageName); |
| 102 | mWidgetType = Preconditions.checkNotNull(widgetType); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sets an optional custom version string for the widget type. |
| 107 | * |
| 108 | * @return this builder |
| 109 | */ |
| 110 | public Builder setWidgetVersion(@Nullable String widgetVersion) { |
| 111 | mWidgetVersion = widgetVersion; |
| 112 | return this; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Builds the text classification context object. |
| 117 | * |
| 118 | * @return the built TextClassificationContext object |
| 119 | */ |
| 120 | @NonNull |
| 121 | public TextClassificationContext build() { |
| 122 | return new TextClassificationContext(mPackageName, mWidgetType, mWidgetVersion); |
| 123 | } |
| 124 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 125 | |
| 126 | @Override |
| 127 | public int describeContents() { |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void writeToParcel(Parcel parcel, int flags) { |
| 133 | parcel.writeString(mPackageName); |
| 134 | parcel.writeString(mWidgetType); |
| 135 | parcel.writeString(mWidgetVersion); |
| 136 | } |
| 137 | |
| 138 | private TextClassificationContext(Parcel in) { |
| 139 | mPackageName = in.readString(); |
| 140 | mWidgetType = in.readString(); |
| 141 | mWidgetVersion = in.readString(); |
| 142 | } |
| 143 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 144 | public static final @android.annotation.NonNull Parcelable.Creator<TextClassificationContext> CREATOR = |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 145 | new Parcelable.Creator<TextClassificationContext>() { |
| 146 | @Override |
| 147 | public TextClassificationContext createFromParcel(Parcel parcel) { |
| 148 | return new TextClassificationContext(parcel); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public TextClassificationContext[] newArray(int size) { |
| 153 | return new TextClassificationContext[size]; |
| 154 | } |
| 155 | }; |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 156 | } |