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.content.pm;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.Objects;
24 
25 /**
26  * Represents a capability that can be performed by an app, also known as App Action.
27  * Capabilities can be associated with a {@link ShortcutInfo}.
28  *
29  * @see ShortcutInfo.Builder#addCapabilityBinding(Capability, CapabilityParams)
30  */
31 public final class Capability implements Parcelable {
32 
33     @NonNull
34     private final String mName;
35 
36     /**
37      * Constructor.
38      * @param name Name of the capability, usually maps to a built-in intent,
39      *            e.g. actions.intent.GET_MESSAGE. Note the character "/" is not permitted.
40      * @throws IllegalArgumentException If specified capability name contains the character "/".
41      *
42      * @hide
43      */
Capability(@onNull final String name)44     Capability(@NonNull final String name) {
45         Objects.requireNonNull(name);
46         if (name.contains("/")) {
47             throw new IllegalArgumentException("'/' is not permitted in the capability name");
48         }
49         mName = name;
50     }
51 
52     /**
53      * Copy constructor.
54      *
55      * @hide
56      */
Capability(@onNull final Capability orig)57     Capability(@NonNull final Capability orig) {
58         this(orig.mName);
59     }
60 
Capability(@onNull final Builder builder)61     private Capability(@NonNull final Builder builder) {
62         this(builder.mName);
63     }
64 
Capability(@onNull final Parcel in)65     private Capability(@NonNull final Parcel in) {
66         mName = in.readString();
67     }
68 
69     /**
70      * Returns the name of the capability. e.g. actions.intent.GET_MESSAGE.
71      */
72     @NonNull
getName()73     public String getName() {
74         return mName;
75     }
76 
77     @Override
equals(Object obj)78     public boolean equals(Object obj) {
79         if (!(obj instanceof Capability)) {
80             return false;
81         }
82         return mName.equals(((Capability) obj).mName);
83     }
84 
85     @Override
hashCode()86     public int hashCode() {
87         return mName.hashCode();
88     }
89 
90     @Override
writeToParcel(@onNull Parcel dest, int flags)91     public void writeToParcel(@NonNull Parcel dest, int flags) {
92         dest.writeString(mName);
93     }
94     @Override
describeContents()95     public int describeContents() {
96         return 0;
97     }
98 
99     @NonNull
100     public static final Parcelable.Creator<Capability> CREATOR =
101             new Parcelable.Creator<Capability>() {
102         @Override
103         public Capability[] newArray(int size) {
104             return new Capability[size];
105         }
106 
107         @Override
108         public Capability createFromParcel(@NonNull Parcel in) {
109             return new Capability(in);
110         }
111     };
112 
113     /**
114      * Builder class for {@link Capability}.
115      */
116     public static final class Builder {
117 
118         @NonNull
119         private final String mName;
120 
121         /**
122          * Constructor.
123          * @param name Name of the capability, usually maps to a built-in intent,
124          *            e.g. actions.intent.GET_MESSAGE. Note the character "/" is not permitted.
125          * @throws IllegalArgumentException If specified capability name contains the character "/".
126          */
Builder(@onNull final String name)127         public Builder(@NonNull final String name) {
128             Objects.requireNonNull(name);
129             if (name.contains("/")) {
130                 throw new IllegalArgumentException("'/' is not permitted in the capability name");
131             }
132             mName = name;
133         }
134 
135         /**
136          * Creates an instance of {@link Capability}
137          */
138         @NonNull
build()139         public Capability build() {
140             return new Capability(this);
141         }
142     }
143 }
144