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.annotation.NonNull; 21 import android.net.wifi.WifiSsid; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.util.ArraySet; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 import java.util.Objects; 29 import java.util.Set; 30 31 /** 32 * Used to indicate the Wi-Fi SSID restriction policy the network must satisfy 33 * in order to be eligible for a connection. 34 * 35 * If the policy type is a denylist, the device may not connect to networks on the denylist. 36 * If the policy type is an allowlist, the device may only connect to networks on the allowlist. 37 * Admin configured networks are not exempt from this restriction. 38 * This policy only prohibits connecting to a restricted network and 39 * does not affect adding a restricted network. 40 * If the current network is present in the denylist or not present in the allowlist, 41 * it will be disconnected. 42 */ 43 public final class WifiSsidPolicy implements Parcelable { 44 /** 45 * SSID policy type indicator for {@link WifiSsidPolicy}. 46 * 47 * <p> When returned from {@link WifiSsidPolicy#getPolicyType()}, the constant 48 * indicates that the SSID policy type is an allowlist. 49 * 50 * @see #WIFI_SSID_POLICY_TYPE_DENYLIST 51 */ 52 public static final int WIFI_SSID_POLICY_TYPE_ALLOWLIST = 0; 53 54 /** 55 * SSID policy type indicator for {@link WifiSsidPolicy}. 56 * 57 * <p> When returned from {@link WifiSsidPolicy#getPolicyType()}, the constant 58 * indicates that the SSID policy type is a denylist. 59 * 60 * @see #WIFI_SSID_POLICY_TYPE_ALLOWLIST 61 */ 62 public static final int WIFI_SSID_POLICY_TYPE_DENYLIST = 1; 63 64 /** 65 * Possible SSID policy types 66 * 67 * @hide */ 68 @Retention(RetentionPolicy.SOURCE) 69 @IntDef(prefix = {"WIFI_SSID_POLICY_TYPE_"}, value = { 70 WIFI_SSID_POLICY_TYPE_ALLOWLIST, 71 WIFI_SSID_POLICY_TYPE_DENYLIST}) 72 public @interface WifiSsidPolicyType {} 73 74 private @WifiSsidPolicyType int mPolicyType; 75 private ArraySet<WifiSsid> mSsids; 76 77 /** 78 * Create the Wi-Fi SSID Policy. 79 * 80 * @param policyType indicate whether the policy is an allowlist or a denylist 81 * @param ssids set of {@link WifiSsid} 82 * @throws IllegalArgumentException if the input ssids set is empty or the policyType is invalid 83 */ WifiSsidPolicy(@ifiSsidPolicyType int policyType, @NonNull Set<WifiSsid> ssids)84 public WifiSsidPolicy(@WifiSsidPolicyType int policyType, @NonNull Set<WifiSsid> ssids) { 85 if (ssids.isEmpty()) { 86 throw new IllegalArgumentException("SSID list cannot be empty"); 87 } 88 if (policyType != WIFI_SSID_POLICY_TYPE_ALLOWLIST 89 && policyType != WIFI_SSID_POLICY_TYPE_DENYLIST) { 90 throw new IllegalArgumentException("Invalid policy type"); 91 } 92 mPolicyType = policyType; 93 mSsids = new ArraySet<>(ssids); 94 } 95 WifiSsidPolicy(Parcel in)96 private WifiSsidPolicy(Parcel in) { 97 mPolicyType = in.readInt(); 98 mSsids = (ArraySet<WifiSsid>) in.readArraySet(null); 99 } 100 101 /** 102 * Returns the set of {@link WifiSsid} 103 */ 104 @NonNull getSsids()105 public Set<WifiSsid> getSsids() { 106 return mSsids; 107 } 108 109 /** 110 * Returns the policy type. 111 */ getPolicyType()112 public @WifiSsidPolicyType int getPolicyType() { 113 return mPolicyType; 114 } 115 116 /** 117 * @see Parcelable.Creator 118 */ 119 @NonNull 120 public static final Creator<WifiSsidPolicy> CREATOR = new Creator<WifiSsidPolicy>() { 121 @Override 122 public WifiSsidPolicy createFromParcel(Parcel source) { 123 return new WifiSsidPolicy(source); 124 } 125 126 @Override 127 public WifiSsidPolicy[] newArray(int size) { 128 return new WifiSsidPolicy[size]; 129 } 130 }; 131 132 @Override writeToParcel(@onNull Parcel dest, int flags)133 public void writeToParcel(@NonNull Parcel dest, int flags) { 134 dest.writeInt(mPolicyType); 135 dest.writeArraySet(mSsids); 136 } 137 138 /** 139 * Two instances of WifiSsidPolicy are considered equal if they have 140 * the same WifiSsidPolicyType and the same set of WifiSsids 141 */ 142 @Override equals(Object thatObject)143 public boolean equals(Object thatObject) { 144 if (this == thatObject) { 145 return true; 146 } 147 if (!(thatObject instanceof WifiSsidPolicy)) { 148 return false; 149 } 150 WifiSsidPolicy that = (WifiSsidPolicy) thatObject; 151 return mPolicyType == that.mPolicyType && Objects.equals(mSsids, that.mSsids); 152 } 153 154 /** 155 * Returns the hash code value of WifiSsidPolicyType and WifiSsid set 156 */ 157 @Override hashCode()158 public int hashCode() { 159 return Objects.hash(mPolicyType, mSsids); 160 } 161 162 @Override describeContents()163 public int describeContents() { 164 return 0; 165 } 166 } 167