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