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 import java.util.Objects;
26 
27 /**
28  * An action defined by the provider that intents into the provider's app for specific
29  * user actions.
30  *
31  * <p>If user selects this action entry, the corresponding {@link PendingIntent} set on the
32  * {@code slice} as a {@link androidx.slice.core.SliceAction} will get invoked.
33  */
34 public final class Action implements Parcelable {
35     /** Slice object containing display content to be displayed with this action on the UI. */
36     @NonNull
37     private final Slice mSlice;
38 
39     /**
40      * Constructs an action to be displayed on the UI.
41      *
42      * <p> Actions must be used for any provider related operations, such as opening the provider
43      * app, intenting straight into certain app activities like 'manage credentials', top
44      * level authentication before displaying any content etc.
45      *
46      * <p> See details on usage of {@code Action} for various actionable entries in
47      * {@link BeginCreateCredentialResponse} and {@link BeginGetCredentialResponse}.
48      *
49      * @param slice the slice containing the metadata to be shown on the UI, must be constructed
50      *              through the {@link androidx.credentials.provider} Jetpack library;
51      *              If constructed manually, the {@code slice} object must
52      *              contain the non-null properties of the
53      *              {@link androidx.credentials.provider.Action} class populated as slice items
54      *              against specific hints as used in the class's {@code toSlice} method,
55      *              since the Android System uses this library to parse the {@code slice} and
56      *              extract the required attributes
57      */
Action(@onNull Slice slice)58     public Action(@NonNull Slice slice) {
59         Objects.requireNonNull(slice, "slice must not be null");
60         mSlice = slice;
61     }
62 
Action(@onNull Parcel in)63     private Action(@NonNull Parcel in) {
64         mSlice = in.readTypedObject(Slice.CREATOR);
65     }
66 
67     @NonNull
68     public static final Creator<Action> CREATOR = new Creator<Action>() {
69         @Override
70         public Action createFromParcel(@NonNull Parcel in) {
71             return new Action(in);
72         }
73 
74         @Override
75         public Action[] newArray(int size) {
76             return new Action[size];
77         }
78     };
79 
80     @Override
describeContents()81     public int describeContents() {
82         return 0;
83     }
84 
85     @Override
writeToParcel(@onNull Parcel dest, int flags)86     public void writeToParcel(@NonNull Parcel dest, int flags) {
87         dest.writeTypedObject(mSlice, flags);
88     }
89 
90     /**
91      * Returns a {@code Slice} object containing the display content to be displayed on the UI.
92      */
93     @NonNull
getSlice()94     public Slice getSlice() {
95         return mSlice;
96     }
97 }
98