1 /*
2  * Copyright (C) 2023 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.app.admin;
18 
19 import static android.app.admin.PolicyUpdateReceiver.EXTRA_PACKAGE_NAME;
20 import static android.app.admin.PolicyUpdateReceiver.EXTRA_POLICY_BUNDLE_KEY;
21 import static android.app.admin.PolicyUpdateReceiver.EXTRA_POLICY_KEY;
22 
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.annotation.SystemApi;
26 import android.annotation.TestApi;
27 import android.os.Bundle;
28 import android.os.Parcel;
29 import android.os.Parcelable;
30 
31 import com.android.modules.utils.TypedXmlPullParser;
32 import com.android.modules.utils.TypedXmlSerializer;
33 
34 import org.xmlpull.v1.XmlPullParserException;
35 
36 import java.io.IOException;
37 import java.util.Objects;
38 
39 /**
40  * Class used to identify a policy that relates to a certain package
41  * (e.g. {@link DevicePolicyManager#setUninstallBlocked}).
42  *
43  * @hide
44  */
45 @SystemApi
46 public final class PackagePolicyKey extends PolicyKey {
47     private static final String ATTR_PACKAGE_NAME = "package-name";
48 
49     private final String mPackageName;
50 
51     /**
52      * @hide
53      */
54     @TestApi
PackagePolicyKey(@onNull String key, @NonNull String packageName)55     public PackagePolicyKey(@NonNull String key, @NonNull String packageName) {
56         super(key);
57         mPackageName = Objects.requireNonNull((packageName));
58     }
59 
PackagePolicyKey(Parcel source)60     private PackagePolicyKey(Parcel source) {
61         super(source.readString());
62         mPackageName = source.readString();
63     }
64 
65     /**
66      * @hide
67      */
PackagePolicyKey(String key)68     public PackagePolicyKey(String key) {
69         super(key);
70         mPackageName = null;
71     }
72 
73     /**
74      * Returns the package name this policy relates to.
75      */
76     @NonNull
getPackageName()77     public String getPackageName() {
78         return mPackageName;
79     }
80 
81     /**
82      * @hide
83      */
84     @Override
saveToXml(TypedXmlSerializer serializer)85     public void saveToXml(TypedXmlSerializer serializer) throws IOException {
86         serializer.attribute(/* namespace= */ null, ATTR_POLICY_IDENTIFIER, getIdentifier());
87         serializer.attribute(/* namespace= */ null, ATTR_PACKAGE_NAME, mPackageName);
88     }
89 
90     /**
91      * @hide
92      */
93     @Override
readFromXml(TypedXmlPullParser parser)94     public PackagePolicyKey readFromXml(TypedXmlPullParser parser)
95             throws XmlPullParserException, IOException {
96         String policyKey = parser.getAttributeValue(/* namespace= */ null,
97                 ATTR_POLICY_IDENTIFIER);
98         String packageName = parser.getAttributeValue(/* namespace= */ null, ATTR_PACKAGE_NAME);
99         return new PackagePolicyKey(policyKey, packageName);
100     }
101 
102     /**
103      * @hide
104      */
105     @Override
writeToBundle(Bundle bundle)106     public void writeToBundle(Bundle bundle) {
107         bundle.putString(EXTRA_POLICY_KEY, getIdentifier());
108         Bundle extraPolicyParams = new Bundle();
109         extraPolicyParams.putString(EXTRA_PACKAGE_NAME, mPackageName);
110         bundle.putBundle(EXTRA_POLICY_BUNDLE_KEY, extraPolicyParams);
111     }
112 
113     @Override
equals(@ullable Object o)114     public boolean equals(@Nullable Object o) {
115         if (this == o) return true;
116         if (o == null || getClass() != o.getClass()) return false;
117         PackagePolicyKey other = (PackagePolicyKey) o;
118         return Objects.equals(getIdentifier(), other.getIdentifier())
119                 && Objects.equals(mPackageName, other.mPackageName);
120     }
121 
122     @Override
hashCode()123     public int hashCode() {
124         return Objects.hash(getIdentifier(), mPackageName);
125     }
126 
127     @Override
toString()128     public String toString() {
129         return "PackagePolicyKey{mPolicyKey= " + getIdentifier()
130                 + "; mPackageName= " + mPackageName + "}";
131     }
132 
133     @Override
describeContents()134     public int describeContents() {
135         return 0;
136     }
137 
138     @Override
writeToParcel(@onNull Parcel dest, int flags)139     public void writeToParcel(@NonNull Parcel dest, int flags) {
140         dest.writeString(getIdentifier());
141         dest.writeString(mPackageName);
142     }
143 
144     @NonNull
145     public static final Parcelable.Creator<PackagePolicyKey> CREATOR =
146             new Parcelable.Creator<PackagePolicyKey>() {
147                 @Override
148                 public PackagePolicyKey createFromParcel(Parcel source) {
149                     return new PackagePolicyKey(source);
150                 }
151 
152                 @Override
153                 public PackagePolicyKey[] newArray(int size) {
154                     return new PackagePolicyKey[size];
155                 }
156             };
157 }
158