blob: fe69f2966de6e841b48ea9c530ac21231c55587e [file] [log] [blame]
Jaewan Kim63461552014-03-10 17:10:51 +09001/*
2 * Copyright (C) 2014 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 android.net;
18
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090019import android.net.StaticIpConfiguration;
Jaewan Kim63461552014-03-10 17:10:51 +090020import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.Objects;
24
25/**
26 * A class representing a configured network.
27 * @hide
28 */
29public class IpConfiguration implements Parcelable {
30 private static final String TAG = "IpConfiguration";
31
32 public enum IpAssignment {
33 /* Use statically configured IP settings. Configuration can be accessed
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090034 * with staticIpConfiguration */
Jaewan Kim63461552014-03-10 17:10:51 +090035 STATIC,
36 /* Use dynamically configured IP settigns */
37 DHCP,
38 /* no IP details are assigned, this is used to indicate
39 * that any existing IP settings should be retained */
40 UNASSIGNED
41 }
42
43 public IpAssignment ipAssignment;
44
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090045 public StaticIpConfiguration staticIpConfiguration;
46
Jaewan Kim63461552014-03-10 17:10:51 +090047 public enum ProxySettings {
48 /* No proxy is to be used. Any existing proxy settings
49 * should be cleared. */
50 NONE,
51 /* Use statically configured proxy. Configuration can be accessed
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090052 * with httpProxy. */
Jaewan Kim63461552014-03-10 17:10:51 +090053 STATIC,
54 /* no proxy details are assigned, this is used to indicate
55 * that any existing proxy settings should be retained */
56 UNASSIGNED,
57 /* Use a Pac based proxy.
58 */
59 PAC
60 }
61
62 public ProxySettings proxySettings;
63
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090064 public ProxyInfo httpProxy;
Jaewan Kim63461552014-03-10 17:10:51 +090065
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090066 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);
Jaewan Kim63461552014-03-10 17:10:51 +090076 }
77
78 public IpConfiguration() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090079 init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
Jaewan Kim63461552014-03-10 17:10:51 +090080 }
81
82 public IpConfiguration(IpAssignment ipAssignment,
83 ProxySettings proxySettings,
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090084 StaticIpConfiguration staticIpConfiguration,
85 ProxyInfo httpProxy) {
86 init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
87 }
88
89 public IpConfiguration(IpConfiguration source) {
90 this();
91 if (source != null) {
92 init(source.ipAssignment, source.proxySettings,
93 source.staticIpConfiguration, source.httpProxy);
94 }
95 }
96
97 public IpAssignment getIpAssignment() {
98 return ipAssignment;
99 }
100
101 public void setIpAssignment(IpAssignment ipAssignment) {
Jaewan Kim63461552014-03-10 17:10:51 +0900102 this.ipAssignment = ipAssignment;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900103 }
104
105 public StaticIpConfiguration getStaticIpConfiguration() {
106 return staticIpConfiguration;
107 }
108
109 public void setStaticIpConfiguration(StaticIpConfiguration staticIpConfiguration) {
110 this.staticIpConfiguration = staticIpConfiguration;
111 }
112
113 public ProxySettings getProxySettings() {
114 return proxySettings;
115 }
116
117 public void setProxySettings(ProxySettings proxySettings) {
Jaewan Kim63461552014-03-10 17:10:51 +0900118 this.proxySettings = proxySettings;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900119 }
120
121 public ProxyInfo getHttpProxy() {
122 return httpProxy;
123 }
124
125 public void setHttpProxy(ProxyInfo httpProxy) {
126 this.httpProxy = httpProxy;
Jaewan Kim63461552014-03-10 17:10:51 +0900127 }
128
129 @Override
130 public String toString() {
131 StringBuilder sbuf = new StringBuilder();
132 sbuf.append("IP assignment: " + ipAssignment.toString());
133 sbuf.append("\n");
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900134 if (staticIpConfiguration != null) {
135 sbuf.append("Static configuration: " + staticIpConfiguration.toString());
136 sbuf.append("\n");
137 }
Jaewan Kim63461552014-03-10 17:10:51 +0900138 sbuf.append("Proxy settings: " + proxySettings.toString());
139 sbuf.append("\n");
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900140 if (httpProxy != null) {
141 sbuf.append("HTTP proxy: " + httpProxy.toString());
142 sbuf.append("\n");
143 }
Jaewan Kim63461552014-03-10 17:10:51 +0900144
145 return sbuf.toString();
146 }
147
148 @Override
149 public boolean equals(Object o) {
150 if (o == this) {
151 return true;
152 }
153
154 if (!(o instanceof IpConfiguration)) {
155 return false;
156 }
157
158 IpConfiguration other = (IpConfiguration) o;
159 return this.ipAssignment == other.ipAssignment &&
160 this.proxySettings == other.proxySettings &&
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900161 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
162 Objects.equals(this.httpProxy, other.httpProxy);
Jaewan Kim63461552014-03-10 17:10:51 +0900163 }
164
165 @Override
166 public int hashCode() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900167 return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
Jaewan Kim63461552014-03-10 17:10:51 +0900168 17 * ipAssignment.ordinal() +
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900169 47 * proxySettings.ordinal() +
170 83 * httpProxy.hashCode();
Jaewan Kim63461552014-03-10 17:10:51 +0900171 }
172
173 /** Implement the Parcelable interface */
174 public int describeContents() {
175 return 0;
176 }
177
178 /** Implement the Parcelable interface */
179 public void writeToParcel(Parcel dest, int flags) {
180 dest.writeString(ipAssignment.name());
181 dest.writeString(proxySettings.name());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900182 dest.writeParcelable(staticIpConfiguration, flags);
183 dest.writeParcelable(httpProxy, flags);
Jaewan Kim63461552014-03-10 17:10:51 +0900184 }
185
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());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900193 config.staticIpConfiguration = in.readParcelable(null);
194 config.httpProxy = in.readParcelable(null);
Jaewan Kim63461552014-03-10 17:10:51 +0900195 return config;
196 }
197
198 public IpConfiguration[] newArray(int size) {
199 return new IpConfiguration[size];
200 }
201 };
202}