1 /* 2 * Copyright 2016, 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.managedprovisioning.model; 18 19 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY; 20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE; 21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_DOMAIN; 22 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_EAP_METHOD; 23 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_HIDDEN; 24 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_IDENTITY; 25 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PAC_URL; 26 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PASSWORD; 27 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PHASE2_AUTH; 28 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS; 29 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_HOST; 30 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_PORT; 31 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE; 32 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID; 33 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE; 34 35 import android.os.Parcel; 36 import android.os.Parcelable; 37 import android.os.PersistableBundle; 38 import android.text.TextUtils; 39 40 import androidx.annotation.Nullable; 41 42 import com.android.internal.annotations.Immutable; 43 import com.android.managedprovisioning.common.PersistableBundlable; 44 45 /** 46 * Stores the WiFi configuration which is used in managed provisioning. 47 */ 48 @Immutable 49 public final class WifiInfo extends PersistableBundlable { 50 public static final boolean DEFAULT_WIFI_HIDDEN = false; 51 public static final int DEFAULT_WIFI_PROXY_PORT = 0; 52 53 public static final Parcelable.Creator<WifiInfo> CREATOR 54 = new Parcelable.Creator<WifiInfo>() { 55 @Override 56 public WifiInfo createFromParcel(Parcel in) { 57 return new WifiInfo(in); 58 } 59 60 @Override 61 public WifiInfo[] newArray(int size) { 62 return new WifiInfo[size]; 63 } 64 }; 65 66 /** Ssid of the wifi network. */ 67 public final String ssid; 68 /** Wifi network in {@link #ssid} is hidden or not. */ 69 public final boolean hidden; 70 /** Security type of the wifi network in {@link #ssid}. */ 71 @Nullable 72 public final String securityType; 73 /** Password of the wifi network in {@link #ssid}. */ 74 @Nullable 75 public final String password; 76 77 /** 78 * EAP method of the wifi network in {@link #ssid}. This is only used if the 79 * {@link #securityType} is {@code EAP}. 80 */ 81 @Nullable 82 public final String eapMethod; 83 84 /** 85 * Phase 2 authentification of the wifi network in {@link #ssid}. This is only used if the 86 * {@link #securityType} is {@code EAP}. 87 */ 88 @Nullable 89 public final String phase2Auth; 90 91 /** 92 * CA certificate of the wifi network in {@link #ssid}. This is only used if the 93 * {@link #securityType} is {@code EAP}. Format of certificate should be as {@link 94 * android.app.admin.DevicePolicyManager#EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE} 95 */ 96 @Nullable 97 public final String caCertificate; 98 99 /** 100 * User certificate of the wifi network in {@link #ssid}. This is only used if the 101 * {@link #securityType} is {@code EAP}. Format of certificate should be as {@link 102 * android.app.admin.DevicePolicyManager#EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE} 103 */ 104 @Nullable 105 public final String userCertificate; 106 107 /** 108 * Identity of the wifi network in {@link #ssid}. This is only used if the 109 * {@link #securityType} is {@code EAP}. 110 */ 111 @Nullable 112 public final String identity; 113 114 /** 115 * Anonymous identity of the wifi network in {@link #ssid}. This is only used if the 116 * {@link #securityType} is {@code EAP}. 117 */ 118 @Nullable 119 public final String anonymousIdentity; 120 121 /** 122 * Domain of the wifi network in {@link #ssid}. This is only used if the 123 * {@link #securityType} is {@code EAP}. 124 */ 125 @Nullable 126 public final String domain; 127 128 /** Proxy host for the wifi network in {@link #ssid}. */ 129 @Nullable 130 public final String proxyHost; 131 /** Proxy port for the wifi network in {@link #ssid}. */ 132 public final int proxyPort; 133 /** The proxy bypass for the wifi network in {@link #ssid}. */ 134 @Nullable 135 public final String proxyBypassHosts; 136 /** The proxy bypass list for the wifi network in {@link #ssid}. */ 137 @Nullable 138 public final String pacUrl; 139 140 @Override toPersistableBundle()141 public PersistableBundle toPersistableBundle() { 142 final PersistableBundle bundle = new PersistableBundle(); 143 bundle.putString(EXTRA_PROVISIONING_WIFI_SSID, ssid); 144 bundle.putBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN, hidden); 145 bundle.putString(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, securityType); 146 bundle.putString(EXTRA_PROVISIONING_WIFI_PASSWORD, password); 147 bundle.putString(EXTRA_PROVISIONING_WIFI_EAP_METHOD, eapMethod); 148 bundle.putString(EXTRA_PROVISIONING_WIFI_PHASE2_AUTH, phase2Auth); 149 bundle.putString(EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE, caCertificate); 150 bundle.putString(EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE, userCertificate); 151 bundle.putString(EXTRA_PROVISIONING_WIFI_IDENTITY, identity); 152 bundle.putString(EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY, anonymousIdentity); 153 bundle.putString(EXTRA_PROVISIONING_WIFI_DOMAIN, domain); 154 bundle.putString(EXTRA_PROVISIONING_WIFI_PROXY_HOST, proxyHost); 155 bundle.putInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT, proxyPort); 156 bundle.putString(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, proxyBypassHosts); 157 bundle.putString(EXTRA_PROVISIONING_WIFI_PAC_URL, pacUrl); 158 return bundle; 159 } 160 fromPersistableBundle(PersistableBundle bundle)161 /* package */ static WifiInfo fromPersistableBundle(PersistableBundle bundle) { 162 return createBuilderFromPersistableBundle(bundle).build(); 163 } 164 createBuilderFromPersistableBundle(PersistableBundle bundle)165 private static Builder createBuilderFromPersistableBundle(PersistableBundle bundle) { 166 Builder builder = new Builder(); 167 builder.setSsid(bundle.getString(EXTRA_PROVISIONING_WIFI_SSID)); 168 builder.setHidden(bundle.getBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN)); 169 builder.setSecurityType(bundle.getString(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE)); 170 builder.setPassword(bundle.getString(EXTRA_PROVISIONING_WIFI_PASSWORD)); 171 builder.setEapMethod(bundle.getString(EXTRA_PROVISIONING_WIFI_EAP_METHOD)); 172 builder.setPhase2Auth(bundle.getString(EXTRA_PROVISIONING_WIFI_PHASE2_AUTH)); 173 builder.setCaCertificate(bundle.getString(EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE)); 174 builder.setUserCertificate(bundle.getString(EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE)); 175 builder.setIdentity(bundle.getString(EXTRA_PROVISIONING_WIFI_IDENTITY)); 176 builder.setAnonymousIdentity(bundle.getString(EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY)); 177 builder.setDomain(bundle.getString(EXTRA_PROVISIONING_WIFI_DOMAIN)); 178 builder.setProxyHost(bundle.getString(EXTRA_PROVISIONING_WIFI_PROXY_HOST)); 179 builder.setProxyPort(bundle.getInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT)); 180 builder.setProxyBypassHosts(bundle.getString(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS)); 181 builder.setPacUrl(bundle.getString(EXTRA_PROVISIONING_WIFI_PAC_URL)); 182 return builder; 183 } 184 WifiInfo(Builder builder)185 private WifiInfo(Builder builder) { 186 ssid = builder.mSsid; 187 hidden = builder.mHidden; 188 securityType = builder.mSecurityType; 189 password = builder.mPassword; 190 eapMethod = builder.eapMethod; 191 phase2Auth = builder.phase2Auth; 192 caCertificate = builder.caCertificate; 193 userCertificate = builder.userCertificate; 194 identity = builder.identity; 195 anonymousIdentity = builder.anonymousIdentity; 196 domain = builder.domain; 197 proxyHost = builder.mProxyHost; 198 proxyPort = builder.mProxyPort; 199 proxyBypassHosts = builder.mProxyBypassHosts; 200 pacUrl = builder.mPacUrl; 201 202 validateFields(); 203 } 204 WifiInfo(Parcel in)205 private WifiInfo(Parcel in) { 206 this(createBuilderFromPersistableBundle( 207 PersistableBundlable.getPersistableBundleFromParcel(in))); 208 } 209 validateFields()210 private void validateFields() { 211 if (TextUtils.isEmpty(ssid)) { 212 throw new IllegalArgumentException("Ssid must not be empty!"); 213 } 214 } 215 216 public final static class Builder { 217 private String mSsid; 218 private boolean mHidden = DEFAULT_WIFI_HIDDEN; 219 private String mSecurityType; 220 private String mPassword; 221 private String eapMethod; 222 private String phase2Auth; 223 private String caCertificate; 224 private String userCertificate; 225 private String identity; 226 private String anonymousIdentity; 227 private String domain; 228 private String mProxyHost; 229 private int mProxyPort = DEFAULT_WIFI_PROXY_PORT; 230 private String mProxyBypassHosts; 231 private String mPacUrl; 232 233 /** 234 * Set the SSID of the network. 235 * 236 * Note: This must be in the same format as {@link android.net.wifi.WifiConfiguration#SSID}, 237 * and must be wrapped in double quotes or else it will be interpreted as hexadecimal. 238 */ setSsid(String ssid)239 public Builder setSsid(String ssid) { 240 mSsid = ssid; 241 return this; 242 } 243 setHidden(boolean hidden)244 public Builder setHidden(boolean hidden) { 245 mHidden = hidden; 246 return this; 247 } 248 setSecurityType(String securityType)249 public Builder setSecurityType(String securityType) { 250 mSecurityType = securityType; 251 return this; 252 } 253 setPassword(String password)254 public Builder setPassword(String password) { 255 mPassword = password; 256 return this; 257 } 258 setEapMethod(String eapMethod)259 public Builder setEapMethod(String eapMethod) { 260 this.eapMethod = eapMethod; 261 return this; 262 } 263 setPhase2Auth(String phase2Auth)264 public Builder setPhase2Auth(String phase2Auth) { 265 this.phase2Auth = phase2Auth; 266 return this; 267 } 268 setCaCertificate(String caCertificate)269 public Builder setCaCertificate(String caCertificate) { 270 this.caCertificate = caCertificate; 271 return this; 272 } 273 setUserCertificate(String userCertificate)274 public Builder setUserCertificate(String userCertificate) { 275 this.userCertificate = userCertificate; 276 return this; 277 } 278 setIdentity(String identity)279 public Builder setIdentity(String identity) { 280 this.identity = identity; 281 return this; 282 } 283 setAnonymousIdentity(String anonymousIdentity)284 public Builder setAnonymousIdentity(String anonymousIdentity) { 285 this.anonymousIdentity = anonymousIdentity; 286 return this; 287 } 288 setDomain(String domain)289 public Builder setDomain(String domain) { 290 this.domain = domain; 291 return this; 292 } 293 setProxyHost(String proxyHost)294 public Builder setProxyHost(String proxyHost) { 295 mProxyHost = proxyHost; 296 return this; 297 } 298 setProxyPort(int proxyPort)299 public Builder setProxyPort(int proxyPort) { 300 mProxyPort = proxyPort; 301 return this; 302 } 303 setProxyBypassHosts(String proxyBypassHosts)304 public Builder setProxyBypassHosts(String proxyBypassHosts) { 305 mProxyBypassHosts = proxyBypassHosts; 306 return this; 307 } 308 setPacUrl(String pacUrl)309 public Builder setPacUrl(String pacUrl) { 310 mPacUrl = pacUrl; 311 return this; 312 } 313 build()314 public WifiInfo build() { 315 return new WifiInfo(this); 316 } 317 builder()318 public static Builder builder() { 319 return new Builder(); 320 } 321 } 322 } 323