1 /*
2  * Copyright 2022 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.credentials;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.annotation.NonNull;
22 import android.os.Bundle;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.internal.util.AnnotationValidations;
27 import com.android.internal.util.Preconditions;
28 
29 /**
30  * Represents a user credential that can be used to authenticate to your app.
31  */
32 public final class Credential implements Parcelable {
33 
34     /**
35      * The type value for password credential related operations.
36      */
37     @NonNull public static final String TYPE_PASSWORD_CREDENTIAL =
38             "android.credentials.TYPE_PASSWORD_CREDENTIAL";
39 
40     /**
41      * The credential type.
42      */
43     @NonNull
44     private final String mType;
45 
46     /**
47      * The credential data.
48      */
49     @NonNull
50     private final Bundle mData;
51 
52     /**
53      * Returns the credential type.
54      */
55     @NonNull
getType()56     public String getType() {
57         return mType;
58     }
59 
60     /**
61      * Returns the credential data.
62      */
63     @NonNull
getData()64     public Bundle getData() {
65         return mData;
66     }
67 
68     @Override
writeToParcel(@onNull Parcel dest, int flags)69     public void writeToParcel(@NonNull Parcel dest, int flags) {
70         dest.writeString8(mType);
71         dest.writeBundle(mData);
72     }
73 
74     @Override
describeContents()75     public int describeContents() {
76         return 0;
77     }
78 
79     @Override
toString()80     public String toString() {
81         return "Credential {" + "type=" + mType + ", data=" + mData + "}";
82     }
83 
84     /**
85      * Constructs a {@link Credential}.
86      *
87      * @param type the type of the credential returned.
88      * @param data the data associated with the credential returned.
89      *
90      * @throws IllegalArgumentException If type is empty.
91      */
Credential(@onNull String type, @NonNull Bundle data)92     public Credential(@NonNull String type, @NonNull Bundle data) {
93         mType = Preconditions.checkStringNotEmpty(type, "type must not be empty");
94         mData = requireNonNull(data, "data must not be null");
95     }
96 
Credential(@onNull Parcel in)97     private Credential(@NonNull Parcel in) {
98         String type = in.readString8();
99         Bundle data = in.readBundle();
100 
101         mType = type;
102         AnnotationValidations.validate(NonNull.class, null, mType);
103         mData = data;
104         AnnotationValidations.validate(NonNull.class, null, mData);
105     }
106 
107     public static final @NonNull Parcelable.Creator<Credential> CREATOR =
108             new Parcelable.Creator<Credential>() {
109         @Override
110         public Credential[] newArray(int size) {
111             return new Credential[size];
112         }
113 
114         @Override
115         public Credential createFromParcel(@NonNull Parcel in) {
116             return new Credential(in);
117         }
118     };
119 }
120