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 com.android.internal.pm.pkg.component;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.text.TextUtils;
24 import android.util.ArraySet;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.internal.util.DataClass;
28 import com.android.internal.util.Parcelling;
29 import com.android.internal.util.Parcelling.BuiltIn.ForInternedString;
30 import com.android.internal.util.Parcelling.BuiltIn.ForStringSet;
31 
32 import java.util.Collections;
33 import java.util.Locale;
34 import java.util.Set;
35 
36 /**
37  * @hide
38  */
39 @DataClass(genGetters = true, genSetters = true, genBuilder = false, genParcelable = false)
40 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
41 public class ParsedPermissionImpl extends ParsedComponentImpl implements ParsedPermission,
42         Parcelable {
43 
44     private static final ForStringSet sForStringSet =
45             Parcelling.Cache.getOrCreate(ForStringSet.class);
46 
47     @Nullable
48     private String backgroundPermission;
49     @Nullable
50     @DataClass.ParcelWith(ForInternedString.class)
51     private String group;
52     private int requestRes;
53     private int protectionLevel;
54     private boolean tree;
55     @Nullable
56     private ParsedPermissionGroup parsedPermissionGroup;
57     @Nullable
58     private Set<String> knownCerts;
59 
60     @VisibleForTesting
ParsedPermissionImpl()61     public ParsedPermissionImpl() {
62     }
63 
getParsedPermissionGroup()64     public ParsedPermissionGroup getParsedPermissionGroup() {
65         return parsedPermissionGroup;
66     }
67 
setGroup(String group)68     public ParsedPermissionImpl setGroup(String group) {
69         this.group = TextUtils.safeIntern(group);
70         return this;
71     }
72 
setKnownCert(String knownCert)73     protected void setKnownCert(String knownCert) {
74         // Convert the provided digest to upper case for consistent Set membership
75         // checks when verifying the signing certificate digests of requesting apps.
76         this.knownCerts = Set.of(knownCert.toUpperCase(Locale.US));
77     }
78 
setKnownCerts(String[] knownCerts)79     protected void setKnownCerts(String[] knownCerts) {
80         this.knownCerts = new ArraySet<>();
81         for (String knownCert : knownCerts) {
82             this.knownCerts.add(knownCert.toUpperCase(Locale.US));
83         }
84     }
85 
86     @NonNull
87     @Override
getKnownCerts()88     public Set<String> getKnownCerts() {
89         return knownCerts == null ? Collections.emptySet() : knownCerts;
90     }
91 
toString()92     public String toString() {
93         return "Permission{"
94                 + Integer.toHexString(System.identityHashCode(this))
95                 + " " + getName() + "}";
96     }
97 
98     @Override
describeContents()99     public int describeContents() {
100         return 0;
101     }
102 
103     @Override
writeToParcel(Parcel dest, int flags)104     public void writeToParcel(Parcel dest, int flags) {
105         super.writeToParcel(dest, flags);
106         dest.writeString(this.backgroundPermission);
107         dest.writeString(this.group);
108         dest.writeInt(this.requestRes);
109         dest.writeInt(this.protectionLevel);
110         dest.writeBoolean(this.tree);
111         dest.writeParcelable((ParsedPermissionGroupImpl) this.parsedPermissionGroup, flags);
112         sForStringSet.parcel(knownCerts, dest, flags);
113     }
114 
ParsedPermissionImpl(Parcel in)115     protected ParsedPermissionImpl(Parcel in) {
116         super(in);
117         this.backgroundPermission = in.readString();
118         this.group = TextUtils.safeIntern(in.readString());
119         this.requestRes = in.readInt();
120         this.protectionLevel = in.readInt();
121         this.tree = in.readBoolean();
122         this.parsedPermissionGroup = in.readParcelable(
123                 ParsedPermissionGroupImpl.class.getClassLoader(), ParsedPermissionGroupImpl.class);
124         this.knownCerts = sForStringSet.unparcel(in);
125     }
126 
127     @NonNull
128     public static final Parcelable.Creator<ParsedPermissionImpl> CREATOR =
129             new Parcelable.Creator<ParsedPermissionImpl>() {
130                 @Override
131                 public ParsedPermissionImpl createFromParcel(Parcel source) {
132                     return new ParsedPermissionImpl(source);
133                 }
134 
135                 @Override
136                 public ParsedPermissionImpl[] newArray(int size) {
137                     return new ParsedPermissionImpl[size];
138                 }
139             };
140 
141 
142 
143     // Code below generated by codegen v1.0.23.
144     //
145     // DO NOT MODIFY!
146     // CHECKSTYLE:OFF Generated code
147     //
148     // To regenerate run:
149     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/com/android/internal/pm/pkg/component/ParsedPermissionImpl.java
150     //
151     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
152     //   Settings > Editor > Code Style > Formatter Control
153     //@formatter:off
154 
155 
156     @DataClass.Generated.Member
ParsedPermissionImpl( @ullable String backgroundPermission, @Nullable String group, int requestRes, int protectionLevel, boolean tree, @Nullable ParsedPermissionGroup parsedPermissionGroup, @Nullable Set<String> knownCerts)157     public ParsedPermissionImpl(
158             @Nullable String backgroundPermission,
159             @Nullable String group,
160             int requestRes,
161             int protectionLevel,
162             boolean tree,
163             @Nullable ParsedPermissionGroup parsedPermissionGroup,
164             @Nullable Set<String> knownCerts) {
165         this.backgroundPermission = backgroundPermission;
166         this.group = group;
167         this.requestRes = requestRes;
168         this.protectionLevel = protectionLevel;
169         this.tree = tree;
170         this.parsedPermissionGroup = parsedPermissionGroup;
171         this.knownCerts = knownCerts;
172 
173         // onConstructed(); // You can define this method to get a callback
174     }
175 
176     @DataClass.Generated.Member
getBackgroundPermission()177     public @Nullable String getBackgroundPermission() {
178         return backgroundPermission;
179     }
180 
181     @DataClass.Generated.Member
getGroup()182     public @Nullable String getGroup() {
183         return group;
184     }
185 
186     @DataClass.Generated.Member
getRequestRes()187     public int getRequestRes() {
188         return requestRes;
189     }
190 
191     @DataClass.Generated.Member
getProtectionLevel()192     public int getProtectionLevel() {
193         return protectionLevel;
194     }
195 
196     @DataClass.Generated.Member
isTree()197     public boolean isTree() {
198         return tree;
199     }
200 
201     @DataClass.Generated.Member
setBackgroundPermission(@onNull String value)202     public @NonNull ParsedPermissionImpl setBackgroundPermission(@NonNull String value) {
203         backgroundPermission = value;
204         return this;
205     }
206 
207     @DataClass.Generated.Member
setRequestRes( int value)208     public @NonNull ParsedPermissionImpl setRequestRes( int value) {
209         requestRes = value;
210         return this;
211     }
212 
213     @DataClass.Generated.Member
setProtectionLevel( int value)214     public @NonNull ParsedPermissionImpl setProtectionLevel( int value) {
215         protectionLevel = value;
216         return this;
217     }
218 
219     @DataClass.Generated.Member
setTree( boolean value)220     public @NonNull ParsedPermissionImpl setTree( boolean value) {
221         tree = value;
222         return this;
223     }
224 
225     @DataClass.Generated.Member
setParsedPermissionGroup(@onNull ParsedPermissionGroup value)226     public @NonNull ParsedPermissionImpl setParsedPermissionGroup(@NonNull ParsedPermissionGroup value) {
227         parsedPermissionGroup = value;
228         return this;
229     }
230 
231     @DataClass.Generated.Member
setKnownCerts(@onNull Set<String> value)232     public @NonNull ParsedPermissionImpl setKnownCerts(@NonNull Set<String> value) {
233         knownCerts = value;
234         return this;
235     }
236 
237     @DataClass.Generated(
238             time = 1701445829812L,
239             codegenVersion = "1.0.23",
240             sourceFile = "frameworks/base/core/java/com/android/internal/pm/pkg/component/ParsedPermissionImpl.java",
241             inputSignatures = "private static final  com.android.internal.util.Parcelling.BuiltIn.ForStringSet sForStringSet\nprivate @android.annotation.Nullable java.lang.String backgroundPermission\nprivate @android.annotation.Nullable @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInternedString.class) java.lang.String group\nprivate  int requestRes\nprivate  int protectionLevel\nprivate  boolean tree\nprivate @android.annotation.Nullable com.android.internal.pm.pkg.component.ParsedPermissionGroup parsedPermissionGroup\nprivate @android.annotation.Nullable java.util.Set<java.lang.String> knownCerts\npublic static final @android.annotation.NonNull android.os.Parcelable.Creator<com.android.internal.pm.pkg.component.ParsedPermissionImpl> CREATOR\npublic  com.android.internal.pm.pkg.component.ParsedPermissionGroup getParsedPermissionGroup()\npublic  com.android.internal.pm.pkg.component.ParsedPermissionImpl setGroup(java.lang.String)\nprotected  void setKnownCert(java.lang.String)\nprotected  void setKnownCerts(java.lang.String[])\npublic @android.annotation.NonNull @java.lang.Override java.util.Set<java.lang.String> getKnownCerts()\npublic  java.lang.String toString()\npublic @java.lang.Override int describeContents()\npublic @java.lang.Override void writeToParcel(android.os.Parcel,int)\nclass ParsedPermissionImpl extends com.android.internal.pm.pkg.component.ParsedComponentImpl implements [com.android.internal.pm.pkg.component.ParsedPermission, android.os.Parcelable]\n@com.android.internal.util.DataClass(genGetters=true, genSetters=true, genBuilder=false, genParcelable=false)")
242     @Deprecated
__metadata()243     private void __metadata() {}
244 
245 
246     //@formatter:on
247     // End of generated code
248 
249 }
250