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.app.admin;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.TestApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Objects;
28 
29 /**
30  * Class to identify a most restrictive resolution mechanism that is used to resolve the enforced
31  * policy when being set by multiple admins (see {@link PolicyState#getResolutionMechanism()}).
32  *
33  * @hide
34  */
35 @TestApi
36 public final class MostRestrictive<V> extends ResolutionMechanism<V> {
37 
38     private final List<PolicyValue<V>> mMostToLeastRestrictive;
39 
40     /**
41      * @hide
42      */
MostRestrictive(@onNull List<PolicyValue<V>> mostToLeastRestrictive)43     public MostRestrictive(@NonNull List<PolicyValue<V>> mostToLeastRestrictive) {
44         mMostToLeastRestrictive = new ArrayList<>(mostToLeastRestrictive);
45     }
46 
47     /**
48      * Returns an ordered list of most to least restrictive values for a certain policy.
49      */
50     @NonNull
getMostToLeastRestrictiveValues()51     public List<V> getMostToLeastRestrictiveValues() {
52         return mMostToLeastRestrictive.stream().map(PolicyValue::getValue).toList();
53     }
54 
55     @Override
equals(@ullable Object o)56     public boolean equals(@Nullable Object o) {
57         if (this == o) return true;
58         if (o == null || getClass() != o.getClass()) return false;
59         try {
60             MostRestrictive<V> other = (MostRestrictive<V>) o;
61             return Objects.equals(mMostToLeastRestrictive, other.mMostToLeastRestrictive);
62         } catch (ClassCastException exception) {
63             return false;
64         }
65     }
66 
67     @Override
hashCode()68     public int hashCode() {
69         return mMostToLeastRestrictive.hashCode();
70     }
71 
72     /**
73      * @hide
74      */
MostRestrictive(Parcel source)75     public MostRestrictive(Parcel source) {
76         mMostToLeastRestrictive = new ArrayList<>();
77         int size = source.readInt();
78         for (int i = 0; i < size; i++) {
79             mMostToLeastRestrictive.add(source.readParcelable(PolicyValue.class.getClassLoader()));
80         }
81     }
82 
83     @Override
toString()84     public String toString() {
85         return "MostRestrictive { mMostToLeastRestrictive= " + mMostToLeastRestrictive + " }";
86     }
87 
88     @Override
describeContents()89     public int describeContents() {
90         return 0;
91     }
92 
93     @Override
writeToParcel(@onNull Parcel dest, int flags)94     public void writeToParcel(@NonNull Parcel dest, int flags) {
95         dest.writeInt(mMostToLeastRestrictive.size());
96         for (PolicyValue<V> entry : mMostToLeastRestrictive) {
97             dest.writeParcelable(entry, flags);
98         }
99     }
100 
101     @NonNull
102     public static final Parcelable.Creator<MostRestrictive<?>> CREATOR =
103             new Parcelable.Creator<MostRestrictive<?>>() {
104                 @Override
105                 public MostRestrictive<?> createFromParcel(Parcel source) {
106                     return new MostRestrictive<>(source);
107                 }
108 
109                 @Override
110                 public MostRestrictive<?>[] newArray(int size) {
111                     return new MostRestrictive[size];
112                 }
113             };
114 }
115