2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.net.StaticIpConfiguration;
20 import android.os.Parcel;
21 import android.os.Parcelable;
23 import java.util.Objects;
26 * A class representing a configured network.
29 public class IpConfiguration implements Parcelable {
30 private static final String TAG = "IpConfiguration";
32 public enum IpAssignment {
33 /* Use statically configured IP settings. Configuration can be accessed
34 * with staticIpConfiguration */
36 /* Use dynamically configured IP settigns */
38 /* no IP details are assigned, this is used to indicate
39 * that any existing IP settings should be retained */
43 public IpAssignment ipAssignment;
45 public StaticIpConfiguration staticIpConfiguration;
47 public enum ProxySettings {
48 /* No proxy is to be used. Any existing proxy settings
49 * should be cleared. */
51 /* Use statically configured proxy. Configuration can be accessed
54 /* no proxy details are assigned, this is used to indicate
55 * that any existing proxy settings should be retained */
57 /* Use a Pac based proxy.
62 public ProxySettings proxySettings;
64 public ProxyInfo httpProxy;
66 private void init(IpAssignment ipAssignment,
67 ProxySettings proxySettings,
68 StaticIpConfiguration staticIpConfiguration,
69 ProxyInfo httpProxy) {
70 this.ipAssignment = ipAssignment;
71 this.proxySettings = proxySettings;
72 this.staticIpConfiguration = (staticIpConfiguration == null) ?
73 null : new StaticIpConfiguration(staticIpConfiguration);
74 this.httpProxy = (httpProxy == null) ?
75 null : new ProxyInfo(httpProxy);
78 public IpConfiguration() {
79 init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
82 public IpConfiguration(IpAssignment ipAssignment,
83 ProxySettings proxySettings,
84 StaticIpConfiguration staticIpConfiguration,
85 ProxyInfo httpProxy) {
86 init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
89 public IpConfiguration(IpConfiguration source) {
92 init(source.ipAssignment, source.proxySettings,
93 source.staticIpConfiguration, source.httpProxy);
97 public IpAssignment getIpAssignment() {
101 public void setIpAssignment(IpAssignment ipAssignment) {
102 this.ipAssignment = ipAssignment;
105 public StaticIpConfiguration getStaticIpConfiguration() {
106 return staticIpConfiguration;
109 public void setStaticIpConfiguration(StaticIpConfiguration staticIpConfiguration) {
110 this.staticIpConfiguration = staticIpConfiguration;
113 public ProxySettings getProxySettings() {
114 return proxySettings;
117 public void setProxySettings(ProxySettings proxySettings) {
118 this.proxySettings = proxySettings;
121 public ProxyInfo getHttpProxy() {
125 public void setHttpProxy(ProxyInfo httpProxy) {
126 this.httpProxy = httpProxy;
130 public String toString() {
131 StringBuilder sbuf = new StringBuilder();
132 sbuf.append("IP assignment: " + ipAssignment.toString());
134 if (staticIpConfiguration != null) {
135 sbuf.append("Static configuration: " + staticIpConfiguration.toString());
138 sbuf.append("Proxy settings: " + proxySettings.toString());
140 if (httpProxy != null) {
141 sbuf.append("HTTP proxy: " + httpProxy.toString());
145 return sbuf.toString();
149 public boolean equals(Object o) {
154 if (!(o instanceof IpConfiguration)) {
158 IpConfiguration other = (IpConfiguration) o;
159 return this.ipAssignment == other.ipAssignment &&
160 this.proxySettings == other.proxySettings &&
161 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
162 Objects.equals(this.httpProxy, other.httpProxy);
166 public int hashCode() {
167 return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
168 17 * ipAssignment.ordinal() +
169 47 * proxySettings.ordinal() +
170 83 * httpProxy.hashCode();
173 /** Implement the Parcelable interface */
174 public int describeContents() {
178 /** Implement the Parcelable interface */
179 public void writeToParcel(Parcel dest, int flags) {
180 dest.writeString(ipAssignment.name());
181 dest.writeString(proxySettings.name());
182 dest.writeParcelable(staticIpConfiguration, flags);
183 dest.writeParcelable(httpProxy, flags);
186 /** Implement the Parcelable interface */
187 public static final Creator<IpConfiguration> CREATOR =
188 new Creator<IpConfiguration>() {
189 public IpConfiguration createFromParcel(Parcel in) {
190 IpConfiguration config = new IpConfiguration();
191 config.ipAssignment = IpAssignment.valueOf(in.readString());
192 config.proxySettings = ProxySettings.valueOf(in.readString());
193 config.staticIpConfiguration = in.readParcelable(null);
194 config.httpProxy = in.readParcelable(null);
198 public IpConfiguration[] newArray(int size) {
199 return new IpConfiguration[size];