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.SystemApi; 22 import android.annotation.TestApi; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import java.util.LinkedHashMap; 27 import java.util.Objects; 28 29 /** 30 * Class containing the state of a certain policy (e.g. all values set by different admins, 31 * current resolved policy, etc). 32 * 33 * <p>Note that the value returned from {@link #getCurrentResolvedPolicy()} might not match any 34 * of the values in {@link #getPoliciesSetByAdmins()} as some policies might be affected by a 35 * conflicting global policy set on the device (retrieved using 36 * {@link DevicePolicyState#getPoliciesForUser} with {@link android.os.UserHandle#ALL}. 37 * 38 * @hide 39 */ 40 @SystemApi 41 public final class PolicyState<V> implements Parcelable { 42 private final LinkedHashMap<EnforcingAdmin, PolicyValue<V>> mPoliciesSetByAdmins = 43 new LinkedHashMap<>(); 44 private PolicyValue<V> mCurrentResolvedPolicy; 45 private ResolutionMechanism<V> mResolutionMechanism; 46 47 /** 48 * @hide 49 */ PolicyState( @onNull LinkedHashMap<EnforcingAdmin, PolicyValue<V>> policiesSetByAdmins, PolicyValue<V> currentEnforcedPolicy, @NonNull ResolutionMechanism<V> resolutionMechanism)50 public PolicyState( 51 @NonNull LinkedHashMap<EnforcingAdmin, PolicyValue<V>> policiesSetByAdmins, 52 PolicyValue<V> currentEnforcedPolicy, 53 @NonNull ResolutionMechanism<V> resolutionMechanism) { 54 Objects.requireNonNull(policiesSetByAdmins); 55 Objects.requireNonNull(resolutionMechanism); 56 57 mPoliciesSetByAdmins.putAll(policiesSetByAdmins); 58 mCurrentResolvedPolicy = currentEnforcedPolicy; 59 mResolutionMechanism = resolutionMechanism; 60 } 61 PolicyState(Parcel source)62 private PolicyState(Parcel source) { 63 int size = source.readInt(); 64 for (int i = 0; i < size; i++) { 65 EnforcingAdmin admin = source.readParcelable(EnforcingAdmin.class.getClassLoader()); 66 PolicyValue<V> policyValue = source.readParcelable(PolicyValue.class.getClassLoader()); 67 mPoliciesSetByAdmins.put(admin, policyValue); 68 } 69 mCurrentResolvedPolicy = source.readParcelable(PolicyValue.class.getClassLoader()); 70 mResolutionMechanism = source.readParcelable(ResolutionMechanism.class.getClassLoader()); 71 } 72 73 /** 74 * Returns all values set by admins for this policy 75 */ 76 @NonNull getPoliciesSetByAdmins()77 public LinkedHashMap<EnforcingAdmin, V> getPoliciesSetByAdmins() { 78 LinkedHashMap<EnforcingAdmin, V> policies = new LinkedHashMap<>(); 79 for (EnforcingAdmin admin : mPoliciesSetByAdmins.keySet()) { 80 policies.put(admin, mPoliciesSetByAdmins.get(admin).getValue()); 81 } 82 return policies; 83 } 84 85 /** 86 * Returns the current resolved policy value. 87 */ 88 @Nullable getCurrentResolvedPolicy()89 public V getCurrentResolvedPolicy() { 90 return mCurrentResolvedPolicy == null ? null : mCurrentResolvedPolicy.getValue(); 91 } 92 93 /** 94 * Returns the resolution mechanism used to resolve the enforced policy when the policy has 95 * been set by multiple enforcing admins {@link EnforcingAdmin}. 96 * 97 * @hide 98 */ 99 @TestApi 100 @NonNull getResolutionMechanism()101 public ResolutionMechanism<V> getResolutionMechanism() { 102 return mResolutionMechanism; 103 } 104 105 @Override toString()106 public String toString() { 107 return "PolicyState { mPoliciesSetByAdmins= " 108 + mPoliciesSetByAdmins + ", mCurrentResolvedPolicy= " + mCurrentResolvedPolicy 109 + ", mResolutionMechanism= " + mResolutionMechanism + " }"; 110 } 111 112 @Override describeContents()113 public int describeContents() { 114 return 0; 115 } 116 117 @Override writeToParcel(@onNull Parcel dest, int flags)118 public void writeToParcel(@NonNull Parcel dest, int flags) { 119 dest.writeInt(mPoliciesSetByAdmins.size()); 120 for (EnforcingAdmin admin : mPoliciesSetByAdmins.keySet()) { 121 dest.writeParcelable(admin, flags); 122 dest.writeParcelable(mPoliciesSetByAdmins.get(admin), flags); 123 } 124 dest.writeParcelable(mCurrentResolvedPolicy, flags); 125 dest.writeParcelable(mResolutionMechanism, flags); 126 } 127 128 @NonNull 129 public static final Parcelable.Creator<PolicyState<?>> CREATOR = 130 new Parcelable.Creator<PolicyState<?>>() { 131 @Override 132 public PolicyState<?> createFromParcel(Parcel source) { 133 return new PolicyState<>(source); 134 } 135 136 @Override 137 public PolicyState<?>[] newArray(int size) { 138 return new PolicyState[size]; 139 } 140 }; 141 } 142