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 top priority resolution mechanism that is used to resolve the enforced
31  * policy when being set by multiple admins (see {@link PolicyState#getResolutionMechanism()}).
32  *
33  * <p>Priorities are defined based on the calling admin's {@link Authority}.
34  *
35  * @hide
36  */
37 @TestApi
38 public final class TopPriority<V> extends ResolutionMechanism<V> {
39 
40     private final List<Authority> mHighestToLowestPriorityAuthorities;
41 
42     /**
43      * @hide
44      */
TopPriority(@onNull List<Authority> highestToLowestPriorityAuthorities)45     public TopPriority(@NonNull List<Authority> highestToLowestPriorityAuthorities) {
46         mHighestToLowestPriorityAuthorities = Objects.requireNonNull(
47                 highestToLowestPriorityAuthorities);
48     }
49 
50     /**
51      * Returns an object with the specified order of highest to lowest authorities.
52      */
TopPriority(@onNull Parcel source)53     private TopPriority(@NonNull Parcel source) {
54         mHighestToLowestPriorityAuthorities = new ArrayList<>();
55         int size = source.readInt();
56         for (int i = 0; i < size; i++) {
57             mHighestToLowestPriorityAuthorities.add(
58                     source.readParcelable(Authority.class.getClassLoader()));
59         }
60     }
61 
62     /**
63      * Returns an ordered list of authorities from highest priority to lowest priority for a
64      * certain policy.
65      */
66     @NonNull
getHighestToLowestPriorityAuthorities()67     public List<Authority> getHighestToLowestPriorityAuthorities() {
68         return mHighestToLowestPriorityAuthorities;
69     }
70 
71     @Override
equals(@ullable Object o)72     public boolean equals(@Nullable Object o) {
73         if (this == o) return true;
74         if (o == null || getClass() != o.getClass()) return false;
75         try {
76             TopPriority<V> other = (TopPriority<V>) o;
77             return Objects.equals(
78                     mHighestToLowestPriorityAuthorities, other.mHighestToLowestPriorityAuthorities);
79         } catch (ClassCastException exception) {
80             return false;
81         }
82     }
83 
84     @Override
hashCode()85     public int hashCode() {
86         return mHighestToLowestPriorityAuthorities.hashCode();
87     }
88 
89     @Override
toString()90     public String toString() {
91         return "TopPriority { mHighestToLowestPriorityAuthorities= "
92                 + mHighestToLowestPriorityAuthorities + " }";
93     }
94     @Override
describeContents()95     public int describeContents() {
96         return 0;
97     }
98 
99     @Override
writeToParcel(@onNull Parcel dest, int flags)100     public void writeToParcel(@NonNull Parcel dest, int flags) {
101         dest.writeInt(mHighestToLowestPriorityAuthorities.size());
102         for (Authority authority : mHighestToLowestPriorityAuthorities) {
103             dest.writeParcelable(authority, flags);
104         }
105     }
106 
107     @NonNull
108     public static final Parcelable.Creator<TopPriority<?>> CREATOR =
109             new Parcelable.Creator<TopPriority<?>>() {
110                 @Override
111                 public TopPriority<?> createFromParcel(Parcel source) {
112                     return new TopPriority<>(source);
113                 }
114 
115                 @Override
116                 public TopPriority<?>[] newArray(int size) {
117                     return new TopPriority[size];
118                 }
119             };
120 
121 }
122