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.Parcel;
23 import android.os.Parcelable;
24 import android.service.credentials.CredentialProviderService;
25 import android.view.autofill.AutofillId;
26 
27 import com.android.internal.util.AnnotationValidations;
28 
29 /**
30  * A response object that encapsulates the credential successfully retrieved from the user.
31  */
32 public final class GetCredentialResponse implements Parcelable {
33 
34     /**
35      * The credential that can be used to authenticate the user.
36      */
37     @NonNull
38     private final Credential mCredential;
39 
40 
41     /**
42      * Returns the credential that can be used to authenticate the user, or {@code null} if no
43      * credential is available.
44      */
45     @NonNull
getCredential()46     public Credential getCredential() {
47         return mCredential;
48     }
49 
50     @Override
writeToParcel(@onNull Parcel dest, int flags)51     public void writeToParcel(@NonNull Parcel dest, int flags) {
52         dest.writeTypedObject(mCredential, flags);
53     }
54 
55     @Override
describeContents()56     public int describeContents() {
57         return 0;
58     }
59 
60     @Override
toString()61     public String toString() {
62         return "GetCredentialResponse {" + "credential=" + mCredential + "}";
63     }
64 
65     /**
66      * @hide
67      */
getAutofillId()68     public AutofillId getAutofillId() {
69         return mCredential.getData().getParcelable(
70                 CredentialProviderService.EXTRA_AUTOFILL_ID,
71                 AutofillId.class);
72     }
73 
74     /**
75      * Constructs a {@link GetCredentialResponse}.
76      *
77      * @param credential the credential successfully retrieved from the user.
78      */
GetCredentialResponse(@onNull Credential credential)79     public GetCredentialResponse(@NonNull Credential credential) {
80         mCredential = requireNonNull(credential, "credential must not be null");
81     }
82 
GetCredentialResponse(@onNull Parcel in)83     private GetCredentialResponse(@NonNull Parcel in) {
84         Credential credential = in.readTypedObject(Credential.CREATOR);
85         mCredential = credential;
86         AnnotationValidations.validate(NonNull.class, null, mCredential);
87     }
88 
89     public static final @NonNull Parcelable.Creator<GetCredentialResponse> CREATOR =
90             new Parcelable.Creator<GetCredentialResponse>() {
91         @Override
92         public GetCredentialResponse[] newArray(int size) {
93             return new GetCredentialResponse[size];
94         }
95 
96         @Override
97         public GetCredentialResponse createFromParcel(@NonNull Parcel in) {
98             return new GetCredentialResponse(in);
99         }
100     };
101 }
102