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_HIDDEN; 20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PAC_URL; 21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PASSWORD; 22 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS; 23 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_HOST; 24 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_PORT; 25 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE; 26 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID; 27 28 29 import android.app.admin.DevicePolicyManager; 30 import android.os.Parcel; 31 import android.os.Parcelable; 32 import android.os.PersistableBundle; 33 import android.support.annotation.Nullable; 34 import android.text.TextUtils; 35 import com.android.internal.annotations.Immutable; 36 import com.android.managedprovisioning.common.PersistableBundlable; 37 import com.android.managedprovisioning.common.StoreUtils; 38 import java.io.IOException; 39 import java.util.Objects; 40 import org.xmlpull.v1.XmlPullParser; 41 import org.xmlpull.v1.XmlPullParserException; 42 import org.xmlpull.v1.XmlSerializer; 43 44 /** 45 * Stores the WiFi configuration which is used in managed provisioning. 46 */ 47 @Immutable 48 public final class WifiInfo extends PersistableBundlable { 49 public static final boolean DEFAULT_WIFI_HIDDEN = false; 50 public static final int DEFAULT_WIFI_PROXY_PORT = 0; 51 52 public static final Parcelable.Creator<WifiInfo> CREATOR 53 = new Parcelable.Creator<WifiInfo>() { 54 @Override 55 public WifiInfo createFromParcel(Parcel in) { 56 return new WifiInfo(in); 57 } 58 59 @Override 60 public WifiInfo[] newArray(int size) { 61 return new WifiInfo[size]; 62 } 63 }; 64 65 /** Ssid of the wifi network. */ 66 public final String ssid; 67 /** Wifi network in {@link #ssid} is hidden or not. */ 68 public final boolean hidden; 69 /** Security type of the wifi network in {@link #ssid}. */ 70 @Nullable 71 public final String securityType; 72 /** Password of the wifi network in {@link #ssid}. */ 73 @Nullable 74 public final String password; 75 /** Proxy host for the wifi network in {@link #ssid}. */ 76 @Nullable 77 public final String proxyHost; 78 /** Proxy port for the wifi network in {@link #ssid}. */ 79 public final int proxyPort; 80 /** The proxy bypass for the wifi network in {@link #ssid}. */ 81 @Nullable 82 public final String proxyBypassHosts; 83 /** The proxy bypass list for the wifi network in {@link #ssid}. */ 84 @Nullable 85 public final String pacUrl; 86 87 @Override toPersistableBundle()88 public PersistableBundle toPersistableBundle() { 89 final PersistableBundle bundle = new PersistableBundle(); 90 bundle.putString(EXTRA_PROVISIONING_WIFI_SSID, ssid); 91 bundle.putBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN, hidden); 92 bundle.putString(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, securityType); 93 bundle.putString(EXTRA_PROVISIONING_WIFI_PASSWORD, password); 94 bundle.putString(EXTRA_PROVISIONING_WIFI_PROXY_HOST, proxyHost); 95 bundle.putInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT, proxyPort); 96 bundle.putString(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, proxyBypassHosts); 97 bundle.putString(EXTRA_PROVISIONING_WIFI_PAC_URL, pacUrl); 98 return bundle; 99 } 100 fromPersistableBundle(PersistableBundle bundle)101 /* package */ static WifiInfo fromPersistableBundle(PersistableBundle bundle) { 102 return createBuilderFromPersistableBundle(bundle).build(); 103 } 104 createBuilderFromPersistableBundle(PersistableBundle bundle)105 private static Builder createBuilderFromPersistableBundle(PersistableBundle bundle) { 106 Builder builder = new Builder(); 107 builder.setSsid(bundle.getString(EXTRA_PROVISIONING_WIFI_SSID)); 108 builder.setHidden(bundle.getBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN)); 109 builder.setSecurityType(bundle.getString(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE)); 110 builder.setPassword(bundle.getString(EXTRA_PROVISIONING_WIFI_PASSWORD)); 111 builder.setProxyHost(bundle.getString(EXTRA_PROVISIONING_WIFI_PROXY_HOST)); 112 builder.setProxyPort(bundle.getInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT)); 113 builder.setProxyBypassHosts(bundle.getString(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS)); 114 builder.setPacUrl(bundle.getString(EXTRA_PROVISIONING_WIFI_PAC_URL)); 115 return builder; 116 } 117 WifiInfo(Builder builder)118 private WifiInfo(Builder builder) { 119 ssid = builder.mSsid; 120 hidden = builder.mHidden; 121 securityType = builder.mSecurityType; 122 password = builder.mPassword; 123 proxyHost = builder.mProxyHost; 124 proxyPort = builder.mProxyPort; 125 proxyBypassHosts = builder.mProxyBypassHosts; 126 pacUrl = builder.mPacUrl; 127 128 validateFields(); 129 } 130 WifiInfo(Parcel in)131 private WifiInfo(Parcel in) { 132 this(createBuilderFromPersistableBundle( 133 PersistableBundlable.getPersistableBundleFromParcel(in))); 134 } 135 validateFields()136 private void validateFields() { 137 if (TextUtils.isEmpty(ssid)) { 138 throw new IllegalArgumentException("Ssid must not be empty!"); 139 } 140 } 141 142 public final static class Builder { 143 private String mSsid; 144 private boolean mHidden = DEFAULT_WIFI_HIDDEN; 145 private String mSecurityType; 146 private String mPassword; 147 private String mProxyHost; 148 private int mProxyPort = DEFAULT_WIFI_PROXY_PORT; 149 private String mProxyBypassHosts; 150 private String mPacUrl; 151 setSsid(String ssid)152 public Builder setSsid(String ssid) { 153 mSsid = ssid; 154 return this; 155 } 156 setHidden(boolean hidden)157 public Builder setHidden(boolean hidden) { 158 mHidden = hidden; 159 return this; 160 } 161 setSecurityType(String securityType)162 public Builder setSecurityType(String securityType) { 163 mSecurityType = securityType; 164 return this; 165 } 166 setPassword(String password)167 public Builder setPassword(String password) { 168 mPassword = password; 169 return this; 170 } 171 setProxyHost(String proxyHost)172 public Builder setProxyHost(String proxyHost) { 173 mProxyHost = proxyHost; 174 return this; 175 } 176 setProxyPort(int proxyPort)177 public Builder setProxyPort(int proxyPort) { 178 mProxyPort = proxyPort; 179 return this; 180 } 181 setProxyBypassHosts(String proxyBypassHosts)182 public Builder setProxyBypassHosts(String proxyBypassHosts) { 183 mProxyBypassHosts = proxyBypassHosts; 184 return this; 185 } 186 setPacUrl(String pacUrl)187 public Builder setPacUrl(String pacUrl) { 188 mPacUrl = pacUrl; 189 return this; 190 } 191 build()192 public WifiInfo build() { 193 return new WifiInfo(this); 194 } 195 builder()196 public static Builder builder() { 197 return new Builder(); 198 } 199 } 200 } 201