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 android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SuppressLint;
22 import android.annotation.SystemApi;
23 import android.os.Bundle;
24 import android.os.Parcelable;
25 
26 import com.android.modules.utils.TypedXmlPullParser;
27 import com.android.modules.utils.TypedXmlSerializer;
28 import android.util.Log;
29 
30 import org.xmlpull.v1.XmlPullParserException;
31 
32 import java.io.IOException;
33 import java.util.Objects;
34 
35 /**
36  * Abstract class used to identify a policy returned from
37  * {@link DevicePolicyManager#getDevicePolicyState()}.
38  *
39  * @hide
40  */
41 // This is ok as the constructor is hidden and all subclasses have implemented Parcelable.
42 @SuppressLint({"ParcelNotFinal", "ParcelCreator"})
43 @SystemApi
44 public abstract class PolicyKey implements Parcelable {
45 
46     static final String TAG = "PolicyKey";
47 
48     /**
49      * @hide
50      */
51     static final String ATTR_POLICY_IDENTIFIER = "policy-identifier";
52 
53     private final String mIdentifier;
54 
55     /**
56      * @hide
57      */
PolicyKey(@onNull String identifier)58     protected PolicyKey(@NonNull String identifier) {
59         mIdentifier = Objects.requireNonNull(identifier);
60     }
61 
62     /**
63      * Returns the string identifier for this policy.
64      */
65     @NonNull
getIdentifier()66     public String getIdentifier() {
67         return mIdentifier;
68     }
69 
70     /**
71      * @hide
72      */
hasSameIdentifierAs(PolicyKey other)73     public boolean hasSameIdentifierAs(PolicyKey other) {
74         if (other == null) {
75             return false;
76         }
77         return mIdentifier.equals(other.mIdentifier);
78     }
79 
80     /**
81      * @hide
82      */
83     @Nullable
readGenericPolicyKeyFromXml(TypedXmlPullParser parser)84     public static PolicyKey readGenericPolicyKeyFromXml(TypedXmlPullParser parser) {
85         String identifier = parser.getAttributeValue(
86                 /* namespace= */ null, ATTR_POLICY_IDENTIFIER);
87         if (identifier == null) {
88             Log.wtf(TAG, "Error parsing generic policy key, identifier is null.");
89             return null;
90         }
91         return new NoArgsPolicyKey(identifier);
92     }
93 
94     /**
95      * @hide
96      */
saveToXml(TypedXmlSerializer serializer)97     public void saveToXml(TypedXmlSerializer serializer) throws IOException {
98         serializer.attribute(/* namespace= */ null, ATTR_POLICY_IDENTIFIER, mIdentifier);
99     }
100 
101     /**
102      * @hide
103      */
readFromXml(TypedXmlPullParser parser)104     public PolicyKey readFromXml(TypedXmlPullParser parser)
105             throws XmlPullParserException, IOException {
106         // No need to read anything
107         return this;
108     }
109 
110     /**
111      * @hide
112      */
writeToBundle(Bundle bundle)113     public abstract void writeToBundle(Bundle bundle);
114 
115     @Override
equals(@ullable Object o)116     public boolean equals(@Nullable Object o) {
117         if (this == o) return true;
118         if (o == null || getClass() != o.getClass()) return false;
119         PolicyKey other = (PolicyKey) o;
120         return Objects.equals(mIdentifier, other.mIdentifier);
121     }
122 
123     @Override
hashCode()124     public int hashCode() {
125         return Objects.hash(mIdentifier);
126     }
127 }
128