1 /*
2  * Copyright (C) 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.service.credentials;
18 
19 import android.annotation.NonNull;
20 import android.app.PendingIntent;
21 import android.app.slice.Slice;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 /**
26  * An entry to be shown on the UI. This entry represents where the credential to be created will
27  * be stored. Examples include user's account, family group etc.
28  *
29  * <p>If user selects this entry, the corresponding {@link PendingIntent} set on the
30  * {@code slice} as a {@link androidx.slice.core.SliceAction} will get invoked.
31  * Once the resulting activity fulfills the required user engagement,
32  * the {@link android.app.Activity} result should be set to {@link android.app.Activity#RESULT_OK},
33  * and the {@link CredentialProviderService#EXTRA_CREATE_CREDENTIAL_RESPONSE} must be set with a
34  * {@link android.credentials.CreateCredentialResponse} object.
35  */
36 public final class CreateEntry implements Parcelable {
37     private final @NonNull Slice mSlice;
38 
CreateEntry(@onNull Parcel in)39     private CreateEntry(@NonNull Parcel in) {
40         mSlice = in.readTypedObject(Slice.CREATOR);
41     }
42 
43     @NonNull
44     public static final Creator<CreateEntry> CREATOR = new Creator<CreateEntry>() {
45         @Override
46         public CreateEntry createFromParcel(@NonNull Parcel in) {
47             return new CreateEntry(in);
48         }
49 
50         @Override
51         public CreateEntry[] newArray(int size) {
52             return new CreateEntry[size];
53         }
54     };
55 
56     @Override
describeContents()57     public int describeContents() {
58         return 0;
59     }
60 
61     @Override
writeToParcel(@onNull Parcel dest, int flags)62     public void writeToParcel(@NonNull Parcel dest, int flags) {
63         dest.writeTypedObject(mSlice, flags);
64     }
65 
66     /**
67      * Constructs a CreateEntry to be displayed on the UI.
68      *
69      * @param slice the slice containing the metadata to be shown on the UI, must be constructed
70      *              through the {@link androidx.credentials.provider} Jetpack library;
71      *              If constructed manually, the {@code slice} object must
72      *              contain the non-null properties of the
73      *              {@link androidx.credentials.provider.CreateEntry} class populated as slice items
74      *              against specific hints as used in the class's {@code toSlice} method,
75      *              since the Android System uses this library to parse the {@code slice} and
76      *              extract the required attributes
77      */
CreateEntry( @onNull Slice slice)78     public CreateEntry(
79             @NonNull Slice slice) {
80         this.mSlice = slice;
81         com.android.internal.util.AnnotationValidations.validate(
82                 NonNull.class, null, mSlice);
83     }
84 
85     /** Returns the content to be displayed with this create entry on the UI. */
86     @NonNull
getSlice()87     public Slice getSlice() {
88         return mSlice;
89     }
90 }
91