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.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.os.Bundle;
23 import android.os.IBinder;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import com.android.internal.util.AnnotationValidations;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 
32 /**
33  * Base dialog result data.
34  *
35  * Returned for simple use cases like cancellation. Can also be subclassed when more information
36  * is needed, e.g. {@link UserSelectionDialogResult}.
37  *
38  * @hide
39  */
40 public class BaseDialogResult implements Parcelable {
41     /** Parses and returns a BaseDialogResult from the given resultData. */
42     @Nullable
fromResultData(@onNull Bundle resultData)43     public static BaseDialogResult fromResultData(@NonNull Bundle resultData) {
44         return resultData.getParcelable(EXTRA_BASE_RESULT, BaseDialogResult.class);
45     }
46 
47     /**
48      * Used for the UX to construct the {@code resultData Bundle} to send via the {@code
49      *  ResultReceiver}.
50      */
addToBundle(@onNull BaseDialogResult result, @NonNull Bundle bundle)51     public static void addToBundle(@NonNull BaseDialogResult result, @NonNull Bundle bundle) {
52         bundle.putParcelable(EXTRA_BASE_RESULT, result);
53     }
54 
55     /**
56      * The intent extra key for the {@code BaseDialogResult} object when the credential
57      * selector activity finishes.
58      */
59     private static final String EXTRA_BASE_RESULT = "android.credentials.ui.extra.BASE_RESULT";
60 
61     /** @hide **/
62     @IntDef(prefix = {"RESULT_CODE_"}, value = {
63             RESULT_CODE_DIALOG_USER_CANCELED,
64             RESULT_CODE_CANCELED_AND_LAUNCHED_SETTINGS,
65             RESULT_CODE_DIALOG_COMPLETE_WITH_SELECTION,
66             RESULT_CODE_DATA_PARSING_FAILURE,
67     })
68     @Retention(RetentionPolicy.SOURCE)
69     public @interface ResultCode {}
70 
71     /** User intentionally canceled the dialog. */
72     public static final int RESULT_CODE_DIALOG_USER_CANCELED = 0;
73     /**
74      * The user has consented to switching to a new default provider. The provider info is in the
75      * {@code resultData}.
76      */
77     public static final int RESULT_CODE_CANCELED_AND_LAUNCHED_SETTINGS = 1;
78     /**
79      * User made a selection and the dialog finished. The user selection result is in the
80      * {@code resultData}.
81      */
82     public static final int RESULT_CODE_DIALOG_COMPLETE_WITH_SELECTION = 2;
83     /**
84      * The UI was canceled because it failed to parse the incoming data.
85      */
86     public static final int RESULT_CODE_DATA_PARSING_FAILURE = 3;
87 
88     @NonNull
89     private final IBinder mRequestToken;
90 
BaseDialogResult(@onNull IBinder requestToken)91     public BaseDialogResult(@NonNull IBinder requestToken) {
92         mRequestToken = requestToken;
93     }
94 
95     /** Returns the unique identifier for the request that launched the operation. */
96     @NonNull
getRequestToken()97     public IBinder getRequestToken() {
98         return mRequestToken;
99     }
100 
BaseDialogResult(@onNull Parcel in)101     protected BaseDialogResult(@NonNull Parcel in) {
102         IBinder requestToken = in.readStrongBinder();
103         mRequestToken = requestToken;
104         AnnotationValidations.validate(NonNull.class, null, mRequestToken);
105     }
106 
107     @Override
writeToParcel(@onNull Parcel dest, int flags)108     public void writeToParcel(@NonNull Parcel dest, int flags) {
109         dest.writeStrongBinder(mRequestToken);
110     }
111 
112     @Override
describeContents()113     public int describeContents() {
114         return 0;
115     }
116 
117     public static final @NonNull Creator<BaseDialogResult> CREATOR =
118             new Creator<BaseDialogResult>() {
119         @Override
120         public BaseDialogResult createFromParcel(@NonNull Parcel in) {
121             return new BaseDialogResult(in);
122         }
123 
124         @Override
125         public BaseDialogResult[] newArray(int size) {
126             return new BaseDialogResult[size];
127         }
128     };
129 }
130