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 android.annotation.Hide;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.content.ComponentName;
23 import android.content.Intent;
24 import android.credentials.selection.GetCredentialProviderData;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 import com.android.internal.util.AnnotationValidations;
29 import com.android.internal.util.Preconditions;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 /**
35  * A list of candidate credentials.
36  *
37  * @hide
38  */
39 @Hide
40 public final class GetCandidateCredentialsResponse implements Parcelable {
41     @NonNull
42     private final List<GetCredentialProviderData> mCandidateProviderDataList;
43 
44     @Nullable
45     private final ComponentName mPrimaryProviderComponentName;
46 
47     @NonNull
48     private final Intent mIntent;
49 
50     /**
51      * @hide
52      */
53     @Hide
GetCandidateCredentialsResponse( @onNull List<GetCredentialProviderData> candidateProviderDataList, @NonNull Intent intent, @Nullable ComponentName primaryProviderComponentName )54     public GetCandidateCredentialsResponse(
55             @NonNull List<GetCredentialProviderData> candidateProviderDataList,
56             @NonNull Intent intent,
57             @Nullable ComponentName primaryProviderComponentName
58     ) {
59         Preconditions.checkCollectionNotEmpty(
60                 candidateProviderDataList,
61                 /*valueName=*/ "candidateProviderDataList");
62         mCandidateProviderDataList = new ArrayList<>(candidateProviderDataList);
63         mIntent = intent;
64         mPrimaryProviderComponentName = primaryProviderComponentName;
65     }
66 
67     /**
68      * Returns candidate provider data list.
69      *
70      * @hide
71      */
getCandidateProviderDataList()72     public List<GetCredentialProviderData> getCandidateProviderDataList() {
73         return mCandidateProviderDataList;
74     }
75 
76     /**
77      * Returns the primary provider component name.
78      *
79      * @hide
80      */
81     @Nullable
getPrimaryProviderComponentName()82     public ComponentName getPrimaryProviderComponentName() {
83         return mPrimaryProviderComponentName;
84     }
85 
86     /**
87      * Returns candidate provider data list.
88      *
89      * @hide
90      */
91     @NonNull
getIntent()92     public Intent getIntent() {
93         return mIntent;
94     }
95 
GetCandidateCredentialsResponse(Parcel in)96     protected GetCandidateCredentialsResponse(Parcel in) {
97         List<GetCredentialProviderData> candidateProviderDataList = new ArrayList<>();
98         in.readTypedList(candidateProviderDataList, GetCredentialProviderData.CREATOR);
99         mCandidateProviderDataList = candidateProviderDataList;
100 
101         AnnotationValidations.validate(NonNull.class, null, mCandidateProviderDataList);
102         mIntent = in.readTypedObject(Intent.CREATOR);
103 
104         mPrimaryProviderComponentName = in.readTypedObject(ComponentName.CREATOR);
105     }
106 
107     @Override
writeToParcel(Parcel dest, int flags)108     public void writeToParcel(Parcel dest, int flags) {
109         dest.writeTypedList(mCandidateProviderDataList);
110         dest.writeTypedObject(mIntent, flags);
111         dest.writeTypedObject(mPrimaryProviderComponentName, flags);
112     }
113 
114     @Override
describeContents()115     public int describeContents() {
116         return 0;
117     }
118 
119     public static final Creator<GetCandidateCredentialsResponse> CREATOR =
120             new Creator<>() {
121                 @Override
122                 public GetCandidateCredentialsResponse createFromParcel(Parcel in) {
123                     return new GetCandidateCredentialsResponse(in);
124                 }
125 
126                 @Override
127                 public GetCandidateCredentialsResponse[] newArray(int size) {
128                     return new GetCandidateCredentialsResponse[size];
129                 }
130             };
131 }
132