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