• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.server.devicepolicy;
18 
19 import android.annotation.NonNull;
20 import android.app.admin.PolicyValue;
21 
22 import java.util.ArrayList;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 
26 final class MostRestrictive<V> extends ResolutionMechanism<V> {
27 
28     private List<PolicyValue<V>> mMostToLeastRestrictive;
29 
MostRestrictive(@onNull List<PolicyValue<V>> mostToLeastRestrictive)30     MostRestrictive(@NonNull List<PolicyValue<V>> mostToLeastRestrictive) {
31         mMostToLeastRestrictive = mostToLeastRestrictive;
32     }
33 
34     @Override
resolve(@onNull LinkedHashMap<EnforcingAdmin, PolicyValue<V>> adminPolicies)35     PolicyValue<V> resolve(@NonNull LinkedHashMap<EnforcingAdmin, PolicyValue<V>> adminPolicies) {
36         return resolve(new ArrayList<>(adminPolicies.values()));
37     }
38 
39     @Override
resolve(List<PolicyValue<V>> adminPolicies)40     PolicyValue<V> resolve(List<PolicyValue<V>> adminPolicies) {
41         if (adminPolicies.isEmpty()) {
42             return null;
43         }
44         for (PolicyValue<V> value : mMostToLeastRestrictive) {
45             if (adminPolicies.contains(value)) {
46                 return value;
47             }
48         }
49         // Return first set policy if none can be found in known values
50         return adminPolicies.get(0);
51     }
52 
53     @Override
getParcelableResolutionMechanism()54     android.app.admin.MostRestrictive<V> getParcelableResolutionMechanism() {
55         return new android.app.admin.MostRestrictive<V>(mMostToLeastRestrictive);
56     }
57 
58     @Override
toString()59     public String toString() {
60         return "MostRestrictive { mMostToLeastRestrictive= " + mMostToLeastRestrictive + " }";
61     }
62 }
63