Debian Codegraph @

core/java/android/view/textclassifier/SystemTextClassifierMetadata.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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.UserIdInt;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23
24 import com.android.internal.annotations.VisibleForTesting;
25 import com.android.internal.annotations.VisibleForTesting.Visibility;
26
27 import java.util.Locale;
28 import java.util.Objects;
29
30 /**
31 * SystemTextClassifier specific information.
32 * <p>
33 * This contains information requires for the TextClassificationManagerService to process the
34 * requests from the application, e.g. user id, calling package name and etc. Centrialize the data
35 * into this class helps to extend the scalability if we want to add new fields.
36 * @hide
37 */
38 @VisibleForTesting(visibility = Visibility.PACKAGE)
39 public final class SystemTextClassifierMetadata implements Parcelable {
40
41 /* The name of the package that sent the TC request. */
42 @NonNull
43 private final String mCallingPackageName;
44 /* The id of the user that sent the TC request. */
45 @UserIdInt
46 private final int mUserId;
47 /* Whether to use the default text classifier to handle the request. */
48 private final boolean mUseDefaultTextClassifier;
49
50 public SystemTextClassifierMetadata(@NonNull String packageName, @UserIdInt int userId,
51 boolean useDefaultTextClassifier) {
52 Objects.requireNonNull(packageName);
53 mCallingPackageName = packageName;
54 mUserId = userId;
55 mUseDefaultTextClassifier = useDefaultTextClassifier;
56 }
57
58 /**
59 * Returns the id of the user that sent the TC request.
60 */
61 @UserIdInt
62 public int getUserId() {
63 return mUserId;
64 }
65
66 /**
67 * Returns the name of the package that sent the TC request.
68 * This returns {@code null} if no calling package name is set.
69 */
70 @NonNull
71 public String getCallingPackageName() {
72 return mCallingPackageName;
73 }
74
75 /**
76 * Returns whether to use the default text classifier to handle TC request.
77 */
78 public boolean useDefaultTextClassifier() {
79 return mUseDefaultTextClassifier;
80 }
81
82 @Override
83 public String toString() {
84 return String.format(Locale.US,
85 "SystemTextClassifierMetadata {callingPackageName=%s, userId=%d, "
86 + "useDefaultTextClassifier=%b}",
87 mCallingPackageName, mUserId, mUseDefaultTextClassifier);
88 }
89
90 private static SystemTextClassifierMetadata readFromParcel(Parcel in) {
91 final String packageName = in.readString();
92 final int userId = in.readInt();
93 final boolean useDefaultTextClassifier = in.readBoolean();
94 return new SystemTextClassifierMetadata(packageName, userId, useDefaultTextClassifier);
95 }
96
97 @Override
98 public int describeContents() {
99 return 0;
100 }
101
102 @Override
103 public void writeToParcel(Parcel dest, int flags) {
104 dest.writeString(mCallingPackageName);
105 dest.writeInt(mUserId);
106 dest.writeBoolean(mUseDefaultTextClassifier);
107 }
108
109 public static final @NonNull Creator<SystemTextClassifierMetadata> CREATOR =
110 new Creator<SystemTextClassifierMetadata>() {
111 @Override
112 public SystemTextClassifierMetadata createFromParcel(Parcel in) {
113 return readFromParcel(in);
114 }
115
116 @Override
117 public SystemTextClassifierMetadata[] newArray(int size) {
118 return new SystemTextClassifierMetadata[size];
119 }
120 };
121 }