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