blob: 99835aa6fb4500cbbd506e04632dc4546077c55c [file] [log] [blame]
Jaewan Kim6d8dcb72014-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
Aaron Huangfb8581a2019-10-02 23:37:02 +080019import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.SuppressLint;
22import android.annotation.SystemApi;
Artur Satayev9c2add62019-12-10 17:47:52 +000023import android.compat.annotation.UnsupportedAppUsage;
Mathew Inwoode1a17ba2020-11-04 09:29:36 +000024import android.os.Build;
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090025import android.os.Parcel;
26import android.os.Parcelable;
27
28import java.util.Objects;
29
30/**
Etan Cohen1e86b3a2021-12-12 02:14:06 +000031 * A class representing the IP configuration of a network.
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090032 */
Aaron Huangfb8581a2019-10-02 23:37:02 +080033public final class IpConfiguration implements Parcelable {
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090034 private static final String TAG = "IpConfiguration";
35
Aaron Huangfb8581a2019-10-02 23:37:02 +080036 // This enum has been used by apps through reflection for many releases.
37 // Therefore they can't just be removed. Duplicating these constants to
38 // give an alternate SystemApi is a worse option than exposing them.
Etan Cohen1e86b3a2021-12-12 02:14:06 +000039 /** @hide */
40 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +080041 @SuppressLint("Enum")
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090042 public enum IpAssignment {
43 /* Use statically configured IP settings. Configuration can be accessed
Lorenzo Colittief734f82014-07-31 00:48:01 +090044 * with staticIpConfiguration */
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090045 STATIC,
Blake Lawson4253b452018-08-23 08:43:07 -070046 /* Use dynamically configured IP settings */
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090047 DHCP,
48 /* no IP details are assigned, this is used to indicate
49 * that any existing IP settings should be retained */
50 UNASSIGNED
51 }
52
Aaron Huangfb8581a2019-10-02 23:37:02 +080053 /** @hide */
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090054 public IpAssignment ipAssignment;
55
Aaron Huangfb8581a2019-10-02 23:37:02 +080056 /** @hide */
Lorenzo Colittief734f82014-07-31 00:48:01 +090057 public StaticIpConfiguration staticIpConfiguration;
58
Aaron Huangfb8581a2019-10-02 23:37:02 +080059 // This enum has been used by apps through reflection for many releases.
60 // Therefore they can't just be removed. Duplicating these constants to
61 // give an alternate SystemApi is a worse option than exposing them.
Etan Cohen1e86b3a2021-12-12 02:14:06 +000062 /** @hide */
63 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +080064 @SuppressLint("Enum")
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090065 public enum ProxySettings {
66 /* No proxy is to be used. Any existing proxy settings
67 * should be cleared. */
68 NONE,
69 /* Use statically configured proxy. Configuration can be accessed
Lorenzo Colittief734f82014-07-31 00:48:01 +090070 * with httpProxy. */
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090071 STATIC,
72 /* no proxy details are assigned, this is used to indicate
73 * that any existing proxy settings should be retained */
74 UNASSIGNED,
75 /* Use a Pac based proxy.
76 */
77 PAC
78 }
79
Aaron Huangfb8581a2019-10-02 23:37:02 +080080 /** @hide */
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090081 public ProxySettings proxySettings;
82
Aaron Huangfb8581a2019-10-02 23:37:02 +080083 /** @hide */
Mathew Inwoodc5b9bec2018-08-08 14:52:47 +010084 @UnsupportedAppUsage
Lorenzo Colittief734f82014-07-31 00:48:01 +090085 public ProxyInfo httpProxy;
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090086
Lorenzo Colittief734f82014-07-31 00:48:01 +090087 private void init(IpAssignment ipAssignment,
88 ProxySettings proxySettings,
89 StaticIpConfiguration staticIpConfiguration,
90 ProxyInfo httpProxy) {
91 this.ipAssignment = ipAssignment;
92 this.proxySettings = proxySettings;
93 this.staticIpConfiguration = (staticIpConfiguration == null) ?
94 null : new StaticIpConfiguration(staticIpConfiguration);
95 this.httpProxy = (httpProxy == null) ?
96 null : new ProxyInfo(httpProxy);
Jaewan Kim6d8dcb72014-03-10 17:10:51 +090097 }
98
Etan Cohen1e86b3a2021-12-12 02:14:06 +000099 /** @hide */
100 @SystemApi
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900101 public IpConfiguration() {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900102 init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900103 }
104
Aaron Huangfb8581a2019-10-02 23:37:02 +0800105 /** @hide */
Mathew Inwoode1a17ba2020-11-04 09:29:36 +0000106 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900107 public IpConfiguration(IpAssignment ipAssignment,
108 ProxySettings proxySettings,
Lorenzo Colittief734f82014-07-31 00:48:01 +0900109 StaticIpConfiguration staticIpConfiguration,
110 ProxyInfo httpProxy) {
111 init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
112 }
113
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000114 /** @hide */
115 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +0800116 public IpConfiguration(@NonNull IpConfiguration source) {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900117 this();
118 if (source != null) {
119 init(source.ipAssignment, source.proxySettings,
120 source.staticIpConfiguration, source.httpProxy);
121 }
122 }
123
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000124 /** @hide */
125 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +0800126 public @NonNull IpAssignment getIpAssignment() {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900127 return ipAssignment;
128 }
129
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000130 /** @hide */
131 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +0800132 public void setIpAssignment(@NonNull IpAssignment ipAssignment) {
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900133 this.ipAssignment = ipAssignment;
Lorenzo Colittief734f82014-07-31 00:48:01 +0900134 }
135
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000136 /**
137 * Get the current static IP configuration (possibly null). Configured via
138 * {@link Builder#setStaticIpConfiguration(StaticIpConfiguration)}.
139 *
140 * @return Current static IP configuration.
141 */
Aaron Huangfb8581a2019-10-02 23:37:02 +0800142 public @Nullable StaticIpConfiguration getStaticIpConfiguration() {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900143 return staticIpConfiguration;
144 }
145
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000146 /** @hide */
147 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +0800148 public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900149 this.staticIpConfiguration = staticIpConfiguration;
150 }
151
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000152 /** @hide */
153 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +0800154 public @NonNull ProxySettings getProxySettings() {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900155 return proxySettings;
156 }
157
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000158 /** @hide */
159 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +0800160 public void setProxySettings(@NonNull ProxySettings proxySettings) {
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900161 this.proxySettings = proxySettings;
Lorenzo Colittief734f82014-07-31 00:48:01 +0900162 }
163
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000164 /**
165 * The proxy configuration of this object.
166 *
167 * @return The proxy information of this object configured via
168 * {@link Builder#setHttpProxy(ProxyInfo)}.
169 */
Aaron Huangfb8581a2019-10-02 23:37:02 +0800170 public @Nullable ProxyInfo getHttpProxy() {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900171 return httpProxy;
172 }
173
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000174 /** @hide */
175 @SystemApi
Aaron Huangfb8581a2019-10-02 23:37:02 +0800176 public void setHttpProxy(@Nullable ProxyInfo httpProxy) {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900177 this.httpProxy = httpProxy;
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900178 }
179
180 @Override
181 public String toString() {
182 StringBuilder sbuf = new StringBuilder();
183 sbuf.append("IP assignment: " + ipAssignment.toString());
184 sbuf.append("\n");
Lorenzo Colittief734f82014-07-31 00:48:01 +0900185 if (staticIpConfiguration != null) {
186 sbuf.append("Static configuration: " + staticIpConfiguration.toString());
187 sbuf.append("\n");
188 }
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900189 sbuf.append("Proxy settings: " + proxySettings.toString());
190 sbuf.append("\n");
Lorenzo Colittief734f82014-07-31 00:48:01 +0900191 if (httpProxy != null) {
192 sbuf.append("HTTP proxy: " + httpProxy.toString());
193 sbuf.append("\n");
194 }
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900195
196 return sbuf.toString();
197 }
198
199 @Override
Roman Kalukiewicz384a8c62020-10-14 15:59:06 -0700200 public boolean equals(@Nullable Object o) {
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900201 if (o == this) {
202 return true;
203 }
204
205 if (!(o instanceof IpConfiguration)) {
206 return false;
207 }
208
209 IpConfiguration other = (IpConfiguration) o;
210 return this.ipAssignment == other.ipAssignment &&
211 this.proxySettings == other.proxySettings &&
Lorenzo Colittief734f82014-07-31 00:48:01 +0900212 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
213 Objects.equals(this.httpProxy, other.httpProxy);
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900214 }
215
216 @Override
217 public int hashCode() {
Lorenzo Colittief734f82014-07-31 00:48:01 +0900218 return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900219 17 * ipAssignment.ordinal() +
Lorenzo Colittief734f82014-07-31 00:48:01 +0900220 47 * proxySettings.ordinal() +
221 83 * httpProxy.hashCode();
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900222 }
223
Aaron Huang4c1dd062019-11-28 14:17:32 +0800224 /** Implement the Parcelable interface */
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900225 public int describeContents() {
226 return 0;
227 }
228
Aaron Huang4c1dd062019-11-28 14:17:32 +0800229 /** Implement the Parcelable interface */
Aaron Huangfb8581a2019-10-02 23:37:02 +0800230 public void writeToParcel(@NonNull Parcel dest, int flags) {
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900231 dest.writeString(ipAssignment.name());
232 dest.writeString(proxySettings.name());
Lorenzo Colittief734f82014-07-31 00:48:01 +0900233 dest.writeParcelable(staticIpConfiguration, flags);
234 dest.writeParcelable(httpProxy, flags);
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900235 }
236
237 /** Implement the Parcelable interface */
Aaron Huangfb8581a2019-10-02 23:37:02 +0800238 public static final @NonNull Creator<IpConfiguration> CREATOR =
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900239 new Creator<IpConfiguration>() {
240 public IpConfiguration createFromParcel(Parcel in) {
241 IpConfiguration config = new IpConfiguration();
242 config.ipAssignment = IpAssignment.valueOf(in.readString());
243 config.proxySettings = ProxySettings.valueOf(in.readString());
Lorenzo Colittief734f82014-07-31 00:48:01 +0900244 config.staticIpConfiguration = in.readParcelable(null);
245 config.httpProxy = in.readParcelable(null);
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900246 return config;
247 }
248
249 public IpConfiguration[] newArray(int size) {
250 return new IpConfiguration[size];
251 }
252 };
Etan Cohen1e86b3a2021-12-12 02:14:06 +0000253
254 /**
255 * Builder used to construct {@link IpConfiguration} objects.
256 */
257 public static final class Builder {
258 private StaticIpConfiguration mStaticIpConfiguration;
259 private ProxyInfo mProxyInfo;
260
261 /**
262 * Set a static IP configuration.
263 *
264 * @param config Static IP configuration.
265 * @return A {@link Builder} object to allow chaining.
266 */
267 public @NonNull Builder setStaticIpConfiguration(@Nullable StaticIpConfiguration config) {
268 mStaticIpConfiguration = config;
269 return this;
270 }
271
272 /**
273 * Set a proxy configuration.
274 *
275 * @param proxyInfo Proxy configuration.
276 * @return A {@link Builder} object to allow chaining.
277 */
278 public @NonNull Builder setHttpProxy(@Nullable ProxyInfo proxyInfo) {
279 mProxyInfo = proxyInfo;
280 return this;
281 }
282
283 /**
284 * Construct an {@link IpConfiguration}.
285 *
286 * @return A new {@link IpConfiguration} object.
287 */
288 public @NonNull IpConfiguration build() {
289 IpConfiguration config = new IpConfiguration();
290 config.setStaticIpConfiguration(mStaticIpConfiguration);
291 config.setIpAssignment(
292 mStaticIpConfiguration == null ? IpAssignment.DHCP : IpAssignment.STATIC);
293
294 config.setHttpProxy(mProxyInfo);
295 if (mProxyInfo == null) {
296 config.setProxySettings(ProxySettings.NONE);
297 } else {
298 config.setProxySettings(
299 mProxyInfo.getPacFileUrl() == null ? ProxySettings.STATIC
300 : ProxySettings.PAC);
301 }
302 return config;
303 }
304 }
Jaewan Kim6d8dcb72014-03-10 17:10:51 +0900305}