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.annotation.SuppressLint;
22 import android.annotation.TestApi;
23 import android.app.PendingIntent;
24 import android.app.slice.Slice;
25 import android.content.Intent;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 
29 import com.android.internal.util.AnnotationValidations;
30 
31 /**
32  * A credential, create, or action entry to be rendered.
33  *
34  * @hide
35  */
36 @TestApi
37 public final class Entry implements Parcelable {
38     @NonNull private final String mKey;
39     @NonNull private final String mSubkey;
40     @Nullable private PendingIntent mPendingIntent;
41     @Nullable private Intent mFrameworkExtrasIntent;
42 
43     @NonNull
44     private final Slice mSlice;
45 
Entry(@onNull Parcel in)46     private Entry(@NonNull Parcel in) {
47         String key = in.readString8();
48         String subkey = in.readString8();
49         Slice slice = in.readTypedObject(Slice.CREATOR);
50 
51         mKey = key;
52         AnnotationValidations.validate(NonNull.class, null, mKey);
53         mSubkey = subkey;
54         AnnotationValidations.validate(NonNull.class, null, mSubkey);
55         mSlice = slice;
56         AnnotationValidations.validate(NonNull.class, null, mSlice);
57         mPendingIntent = in.readTypedObject(PendingIntent.CREATOR);
58         mFrameworkExtrasIntent = in.readTypedObject(Intent.CREATOR);
59     }
60 
61     /** Constructor to be used for an entry that does not require further activities
62      * to be invoked when selected.
63      */
Entry(@onNull String key, @NonNull String subkey, @NonNull Slice slice)64     public Entry(@NonNull String key, @NonNull String subkey, @NonNull Slice slice) {
65         mKey = key;
66         mSubkey = subkey;
67         mSlice = slice;
68     }
69 
70     /** Constructor to be used for an entry that requires a pending intent to be invoked
71      * when clicked.
72      */
Entry(@onNull String key, @NonNull String subkey, @NonNull Slice slice, @NonNull Intent intent)73     public Entry(@NonNull String key, @NonNull String subkey, @NonNull Slice slice,
74             @NonNull Intent intent) {
75         this(key, subkey, slice);
76         mFrameworkExtrasIntent = intent;
77     }
78 
79     /**
80     * Returns the identifier of this entry that's unique within the context of the CredentialManager
81     * request.
82     */
83     @NonNull
getKey()84     public String getKey() {
85         return mKey;
86     }
87 
88     /**
89      * Returns the sub-identifier of this entry that's unique within the context of the {@code key}.
90      */
91     @NonNull
getSubkey()92     public String getSubkey() {
93         return mSubkey;
94     }
95 
96     /**
97     * Returns the Slice to be rendered.
98     */
99     @NonNull
getSlice()100     public Slice getSlice() {
101         return mSlice;
102     }
103 
104     @Nullable
getPendingIntent()105     public PendingIntent getPendingIntent() {
106         return mPendingIntent;
107     }
108 
109     @Nullable
110     @SuppressLint("IntentBuilderName") // Not building a new intent.
getFrameworkExtrasIntent()111     public Intent getFrameworkExtrasIntent() {
112         return mFrameworkExtrasIntent;
113     }
114 
115     @Override
writeToParcel(@onNull Parcel dest, int flags)116     public void writeToParcel(@NonNull Parcel dest, int flags) {
117         dest.writeString8(mKey);
118         dest.writeString8(mSubkey);
119         dest.writeTypedObject(mSlice, flags);
120         dest.writeTypedObject(mPendingIntent, flags);
121         dest.writeTypedObject(mFrameworkExtrasIntent, flags);
122     }
123 
124     @Override
describeContents()125     public int describeContents() {
126         return 0;
127     }
128 
129     public static final @NonNull Creator<Entry> CREATOR = new Creator<Entry>() {
130         @Override
131         public Entry createFromParcel(@NonNull Parcel in) {
132             return new Entry(in);
133         }
134 
135         @Override
136         public Entry[] newArray(int size) {
137             return new Entry[size];
138         }
139     };
140 }
141