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.ui;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Bundle;
22 import android.os.IBinder;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.internal.util.AnnotationValidations;
27 
28 /**
29  * Result data matching {@link BaseDialogResult#RESULT_CODE_DIALOG_COMPLETE_WITH_SELECTION}.
30  *
31  * @hide
32  */
33 public final class UserSelectionDialogResult extends BaseDialogResult implements Parcelable {
34     /** Parses and returns a UserSelectionDialogResult from the given resultData. */
35     @Nullable
fromResultData(@onNull Bundle resultData)36     public static UserSelectionDialogResult fromResultData(@NonNull Bundle resultData) {
37         return resultData.getParcelable(
38             EXTRA_USER_SELECTION_RESULT, UserSelectionDialogResult.class);
39     }
40 
41     /**
42      * Used for the UX to construct the {@code resultData Bundle} to send via the {@code
43      *  ResultReceiver}.
44      */
addToBundle( @onNull UserSelectionDialogResult result, @NonNull Bundle bundle)45     public static void addToBundle(
46             @NonNull UserSelectionDialogResult result, @NonNull Bundle bundle) {
47         bundle.putParcelable(EXTRA_USER_SELECTION_RESULT, result);
48     }
49 
50     /**
51      * The intent extra key for the {@code UserSelectionDialogResult} object when the credential
52      * selector activity finishes.
53      */
54     private static final String EXTRA_USER_SELECTION_RESULT =
55             "android.credentials.ui.extra.USER_SELECTION_RESULT";
56 
57     @NonNull private final String mProviderId;
58     @NonNull private final String mEntryKey;
59     @NonNull private final String mEntrySubkey;
60     @Nullable private ProviderPendingIntentResponse mProviderPendingIntentResponse;
61 
UserSelectionDialogResult( @onNull IBinder requestToken, @NonNull String providerId, @NonNull String entryKey, @NonNull String entrySubkey)62     public UserSelectionDialogResult(
63             @NonNull IBinder requestToken, @NonNull String providerId,
64             @NonNull String entryKey, @NonNull String entrySubkey) {
65         super(requestToken);
66         mProviderId = providerId;
67         mEntryKey = entryKey;
68         mEntrySubkey = entrySubkey;
69     }
70 
UserSelectionDialogResult( @onNull IBinder requestToken, @NonNull String providerId, @NonNull String entryKey, @NonNull String entrySubkey, @Nullable ProviderPendingIntentResponse providerPendingIntentResponse)71     public UserSelectionDialogResult(
72             @NonNull IBinder requestToken, @NonNull String providerId,
73             @NonNull String entryKey, @NonNull String entrySubkey,
74             @Nullable ProviderPendingIntentResponse providerPendingIntentResponse) {
75         super(requestToken);
76         mProviderId = providerId;
77         mEntryKey = entryKey;
78         mEntrySubkey = entrySubkey;
79         mProviderPendingIntentResponse = providerPendingIntentResponse;
80     }
81 
82     /** Returns provider package name whose entry was selected by the user. */
83     @NonNull
getProviderId()84     public String getProviderId() {
85         return mProviderId;
86     }
87 
88     /** Returns the key of the visual entry that the user selected. */
89     @NonNull
getEntryKey()90     public String getEntryKey() {
91         return mEntryKey;
92     }
93 
94     /** Returns the subkey of the visual entry that the user selected. */
95     @NonNull
getEntrySubkey()96     public String getEntrySubkey() {
97         return mEntrySubkey;
98     }
99 
100     /** Returns the pending intent response from the provider. */
101     @Nullable
getPendingIntentProviderResponse()102     public ProviderPendingIntentResponse getPendingIntentProviderResponse() {
103         return mProviderPendingIntentResponse;
104     }
105 
UserSelectionDialogResult(@onNull Parcel in)106     protected UserSelectionDialogResult(@NonNull Parcel in) {
107         super(in);
108         String providerId = in.readString8();
109         String entryKey = in.readString8();
110         String entrySubkey = in.readString8();
111 
112         mProviderId = providerId;
113         AnnotationValidations.validate(NonNull.class, null, mProviderId);
114         mEntryKey = entryKey;
115         AnnotationValidations.validate(NonNull.class, null, mEntryKey);
116         mEntrySubkey = entrySubkey;
117         AnnotationValidations.validate(NonNull.class, null, mEntrySubkey);
118         mProviderPendingIntentResponse = in.readTypedObject(ProviderPendingIntentResponse.CREATOR);
119     }
120 
121     @Override
writeToParcel(@onNull Parcel dest, int flags)122     public void writeToParcel(@NonNull Parcel dest, int flags) {
123         super.writeToParcel(dest, flags);
124         dest.writeString8(mProviderId);
125         dest.writeString8(mEntryKey);
126         dest.writeString8(mEntrySubkey);
127         dest.writeTypedObject(mProviderPendingIntentResponse, flags);
128     }
129 
130     @Override
describeContents()131     public int describeContents() {
132         return 0;
133     }
134 
135     public static final @NonNull Creator<UserSelectionDialogResult> CREATOR =
136             new Creator<UserSelectionDialogResult>() {
137         @Override
138         public UserSelectionDialogResult createFromParcel(@NonNull Parcel in) {
139             return new UserSelectionDialogResult(in);
140         }
141 
142         @Override
143         public UserSelectionDialogResult[] newArray(int size) {
144             return new UserSelectionDialogResult[size];
145         }
146     };
147 }
148