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.IntDef; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.text.TextUtils; 23 import android.util.Log; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 28 import com.android.modules.utils.TypedXmlPullParser; 29 import com.android.modules.utils.TypedXmlSerializer; 30 31 import java.io.IOException; 32 import java.lang.annotation.Retention; 33 import java.lang.annotation.RetentionPolicy; 34 import java.util.Objects; 35 36 /** 37 * A policy class that describes how managed SIM subscriptions should behave on the device. 38 */ 39 public final class ManagedSubscriptionsPolicy implements Parcelable { 40 private static final String TAG = "ManagedSubscriptionsPolicy"; 41 42 /** @hide */ 43 @Retention(RetentionPolicy.SOURCE) 44 @IntDef(prefix = {"TYPE_"}, value = {TYPE_ALL_PERSONAL_SUBSCRIPTIONS, 45 TYPE_ALL_MANAGED_SUBSCRIPTIONS}) 46 @interface ManagedSubscriptionsPolicyType { 47 } 48 49 /** 50 * Represents default policy to not have any managed subscriptions on the device. 51 */ 52 public static final int TYPE_ALL_PERSONAL_SUBSCRIPTIONS = 0; 53 54 /** 55 * Represents policy to have only managed subscriptions on the device, any existing and 56 * future subscriptions on the device are exclusively associated with the managed profile. 57 * 58 * <p>When a subscription is associated with the managed profile, incoming/outgoing calls and 59 * text message using that subscription would only work via apps on managed profile. 60 * Also, Call logs and messages would be accessible only from the managed profile. 61 */ 62 public static final int TYPE_ALL_MANAGED_SUBSCRIPTIONS = 1; 63 64 @ManagedSubscriptionsPolicyType 65 private final int mPolicyType; 66 67 private static final String KEY_POLICY_TYPE = "policy_type"; 68 ManagedSubscriptionsPolicy(@anagedSubscriptionsPolicyType int policyType)69 public ManagedSubscriptionsPolicy(@ManagedSubscriptionsPolicyType int policyType) { 70 if (policyType != TYPE_ALL_PERSONAL_SUBSCRIPTIONS 71 && policyType != TYPE_ALL_MANAGED_SUBSCRIPTIONS) { 72 throw new IllegalArgumentException("Invalid policy type"); 73 } 74 mPolicyType = policyType; 75 } 76 77 @NonNull 78 public static final Parcelable.Creator<ManagedSubscriptionsPolicy> CREATOR = 79 new Parcelable.Creator<ManagedSubscriptionsPolicy>() { 80 public ManagedSubscriptionsPolicy createFromParcel(Parcel in) { 81 ManagedSubscriptionsPolicy policy = new ManagedSubscriptionsPolicy( 82 in.readInt()); 83 return policy; 84 } 85 86 @Override 87 public ManagedSubscriptionsPolicy[] newArray(int size) { 88 return new ManagedSubscriptionsPolicy[size]; 89 } 90 }; 91 92 /** 93 * Returns the type of managed subscriptions policy, or {@link #TYPE_ALL_PERSONAL_SUBSCRIPTIONS} 94 * if no policy has been set. 95 * 96 * @return The policy type. 97 */ 98 @ManagedSubscriptionsPolicyType getPolicyType()99 public int getPolicyType() { 100 return mPolicyType; 101 } 102 103 @Override toString()104 public String toString() { 105 return TextUtils.formatSimple("ManagedSubscriptionsPolicy (type: %d)", mPolicyType); 106 } 107 108 @Override describeContents()109 public int describeContents() { 110 return 0; 111 } 112 113 @Override writeToParcel(@onNull Parcel dest, int flags)114 public void writeToParcel(@NonNull Parcel dest, int flags) { 115 dest.writeInt(mPolicyType); 116 } 117 118 @Override equals(Object thatObject)119 public boolean equals(Object thatObject) { 120 if (this == thatObject) { 121 return true; 122 } 123 if (!(thatObject instanceof ManagedSubscriptionsPolicy)) { 124 return false; 125 } 126 ManagedSubscriptionsPolicy that = (ManagedSubscriptionsPolicy) thatObject; 127 return mPolicyType == that.mPolicyType; 128 } 129 130 @Override hashCode()131 public int hashCode() { 132 return Objects.hash(mPolicyType); 133 } 134 135 /** @hide */ 136 @Nullable readFromXml(@onNull TypedXmlPullParser parser)137 public static ManagedSubscriptionsPolicy readFromXml(@NonNull TypedXmlPullParser parser) { 138 try { 139 ManagedSubscriptionsPolicy policy = new ManagedSubscriptionsPolicy( 140 parser.getAttributeInt(null, KEY_POLICY_TYPE, -1)); 141 142 return policy; 143 } catch (IllegalArgumentException e) { 144 // Fail through 145 Log.w(TAG, "Load xml failed", e); 146 } 147 return null; 148 } 149 150 /** 151 * @hide 152 */ saveToXml(TypedXmlSerializer out)153 public void saveToXml(TypedXmlSerializer out) throws IOException { 154 out.attributeInt(null, KEY_POLICY_TYPE, mPolicyType); 155 } 156 } 157