blob: d5f8b2edb6bbda24dfd22f1e89e809e8bde8b20a [file] [log] [blame]
Remi NGUYEN VANfbbccbc2021-01-15 18:08:24 +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
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.SuppressLint;
22import android.annotation.SystemApi;
23import android.compat.annotation.UnsupportedAppUsage;
24import android.os.Build;
25import android.os.Parcel;
26import android.os.Parcelable;
27
28import java.util.Objects;
29
30/**
31 * A class representing a configured network.
32 * @hide
33 */
34@SystemApi
35public final class IpConfiguration implements Parcelable {
36 private static final String TAG = "IpConfiguration";
37
38 // This enum has been used by apps through reflection for many releases.
39 // Therefore they can't just be removed. Duplicating these constants to
40 // give an alternate SystemApi is a worse option than exposing them.
41 @SuppressLint("Enum")
42 public enum IpAssignment {
43 /* Use statically configured IP settings. Configuration can be accessed
44 * with staticIpConfiguration */
45 STATIC,
46 /* Use dynamically configured IP settings */
47 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
53 /** @hide */
54 public IpAssignment ipAssignment;
55
56 /** @hide */
57 public StaticIpConfiguration staticIpConfiguration;
58
59 // 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.
62 @SuppressLint("Enum")
63 public enum ProxySettings {
64 /* No proxy is to be used. Any existing proxy settings
65 * should be cleared. */
66 NONE,
67 /* Use statically configured proxy. Configuration can be accessed
68 * with httpProxy. */
69 STATIC,
70 /* no proxy details are assigned, this is used to indicate
71 * that any existing proxy settings should be retained */
72 UNASSIGNED,
73 /* Use a Pac based proxy.
74 */
75 PAC
76 }
77
78 /** @hide */
79 public ProxySettings proxySettings;
80
81 /** @hide */
82 @UnsupportedAppUsage
83 public ProxyInfo httpProxy;
84
85 private void init(IpAssignment ipAssignment,
86 ProxySettings proxySettings,
87 StaticIpConfiguration staticIpConfiguration,
88 ProxyInfo httpProxy) {
89 this.ipAssignment = ipAssignment;
90 this.proxySettings = proxySettings;
91 this.staticIpConfiguration = (staticIpConfiguration == null) ?
92 null : new StaticIpConfiguration(staticIpConfiguration);
93 this.httpProxy = (httpProxy == null) ?
94 null : new ProxyInfo(httpProxy);
95 }
96
97 public IpConfiguration() {
98 init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
99 }
100
101 /** @hide */
102 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
103 public IpConfiguration(IpAssignment ipAssignment,
104 ProxySettings proxySettings,
105 StaticIpConfiguration staticIpConfiguration,
106 ProxyInfo httpProxy) {
107 init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
108 }
109
110 public IpConfiguration(@NonNull IpConfiguration source) {
111 this();
112 if (source != null) {
113 init(source.ipAssignment, source.proxySettings,
114 source.staticIpConfiguration, source.httpProxy);
115 }
116 }
117
118 public @NonNull IpAssignment getIpAssignment() {
119 return ipAssignment;
120 }
121
122 public void setIpAssignment(@NonNull IpAssignment ipAssignment) {
123 this.ipAssignment = ipAssignment;
124 }
125
126 public @Nullable StaticIpConfiguration getStaticIpConfiguration() {
127 return staticIpConfiguration;
128 }
129
130 public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) {
131 this.staticIpConfiguration = staticIpConfiguration;
132 }
133
134 public @NonNull ProxySettings getProxySettings() {
135 return proxySettings;
136 }
137
138 public void setProxySettings(@NonNull ProxySettings proxySettings) {
139 this.proxySettings = proxySettings;
140 }
141
142 public @Nullable ProxyInfo getHttpProxy() {
143 return httpProxy;
144 }
145
146 public void setHttpProxy(@Nullable ProxyInfo httpProxy) {
147 this.httpProxy = httpProxy;
148 }
149
150 @Override
151 public String toString() {
152 StringBuilder sbuf = new StringBuilder();
153 sbuf.append("IP assignment: " + ipAssignment.toString());
154 sbuf.append("\n");
155 if (staticIpConfiguration != null) {
156 sbuf.append("Static configuration: " + staticIpConfiguration.toString());
157 sbuf.append("\n");
158 }
159 sbuf.append("Proxy settings: " + proxySettings.toString());
160 sbuf.append("\n");
161 if (httpProxy != null) {
162 sbuf.append("HTTP proxy: " + httpProxy.toString());
163 sbuf.append("\n");
164 }
165
166 return sbuf.toString();
167 }
168
169 @Override
Remi NGUYEN VANa522fc22021-02-01 10:25:24 +0000170 public boolean equals(@Nullable Object o) {
Remi NGUYEN VANfbbccbc2021-01-15 18:08:24 +0900171 if (o == this) {
172 return true;
173 }
174
175 if (!(o instanceof IpConfiguration)) {
176 return false;
177 }
178
179 IpConfiguration other = (IpConfiguration) o;
180 return this.ipAssignment == other.ipAssignment &&
181 this.proxySettings == other.proxySettings &&
182 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
183 Objects.equals(this.httpProxy, other.httpProxy);
184 }
185
186 @Override
187 public int hashCode() {
188 return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
189 17 * ipAssignment.ordinal() +
190 47 * proxySettings.ordinal() +
191 83 * httpProxy.hashCode();
192 }
193
194 /** Implement the Parcelable interface */
195 public int describeContents() {
196 return 0;
197 }
198
199 /** Implement the Parcelable interface */
200 public void writeToParcel(@NonNull Parcel dest, int flags) {
201 dest.writeString(ipAssignment.name());
202 dest.writeString(proxySettings.name());
203 dest.writeParcelable(staticIpConfiguration, flags);
204 dest.writeParcelable(httpProxy, flags);
205 }
206
207 /** Implement the Parcelable interface */
208 public static final @NonNull Creator<IpConfiguration> CREATOR =
209 new Creator<IpConfiguration>() {
210 public IpConfiguration createFromParcel(Parcel in) {
211 IpConfiguration config = new IpConfiguration();
212 config.ipAssignment = IpAssignment.valueOf(in.readString());
213 config.proxySettings = ProxySettings.valueOf(in.readString());
214 config.staticIpConfiguration = in.readParcelable(null);
215 config.httpProxy = in.readParcelable(null);
216 return config;
217 }
218
219 public IpConfiguration[] newArray(int size) {
220 return new IpConfiguration[size];
221 }
222 };
223}