blob: dddb64d8cecec06d786b76689f6ffd56c13bf7f1 [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
Aaron Huang6763aec2019-10-02 23:37:02 +080019import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.SuppressLint;
22import android.annotation.SystemApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010023import android.annotation.UnsupportedAppUsage;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090024import android.net.StaticIpConfiguration;
Jaewan Kim63461552014-03-10 17:10:51 +090025import android.os.Parcel;
26import android.os.Parcelable;
27
28import java.util.Objects;
29
30/**
31 * A class representing a configured network.
32 * @hide
33 */
Aaron Huang6763aec2019-10-02 23:37:02 +080034@SystemApi
35public final class IpConfiguration implements Parcelable {
Jaewan Kim63461552014-03-10 17:10:51 +090036 private static final String TAG = "IpConfiguration";
37
Aaron Huang6763aec2019-10-02 23:37:02 +080038 // 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")
Jaewan Kim63461552014-03-10 17:10:51 +090042 public enum IpAssignment {
43 /* Use statically configured IP settings. Configuration can be accessed
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090044 * with staticIpConfiguration */
Jaewan Kim63461552014-03-10 17:10:51 +090045 STATIC,
Blake Lawson0c9ed962018-08-23 08:43:07 -070046 /* Use dynamically configured IP settings */
Jaewan Kim63461552014-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 Huang6763aec2019-10-02 23:37:02 +080053 /** @hide */
Jaewan Kim63461552014-03-10 17:10:51 +090054 public IpAssignment ipAssignment;
55
Aaron Huang6763aec2019-10-02 23:37:02 +080056 /** @hide */
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090057 public StaticIpConfiguration staticIpConfiguration;
58
Aaron Huang6763aec2019-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.
62 @SuppressLint("Enum")
Jaewan Kim63461552014-03-10 17:10:51 +090063 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
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090068 * with httpProxy. */
Jaewan Kim63461552014-03-10 17:10:51 +090069 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
Aaron Huang6763aec2019-10-02 23:37:02 +080078 /** @hide */
Jaewan Kim63461552014-03-10 17:10:51 +090079 public ProxySettings proxySettings;
80
Aaron Huang6763aec2019-10-02 23:37:02 +080081 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010082 @UnsupportedAppUsage
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090083 public ProxyInfo httpProxy;
Jaewan Kim63461552014-03-10 17:10:51 +090084
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090085 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);
Jaewan Kim63461552014-03-10 17:10:51 +090095 }
96
97 public IpConfiguration() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090098 init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
Jaewan Kim63461552014-03-10 17:10:51 +090099 }
100
Aaron Huang6763aec2019-10-02 23:37:02 +0800101 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100102 @UnsupportedAppUsage
Jaewan Kim63461552014-03-10 17:10:51 +0900103 public IpConfiguration(IpAssignment ipAssignment,
104 ProxySettings proxySettings,
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900105 StaticIpConfiguration staticIpConfiguration,
106 ProxyInfo httpProxy) {
107 init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
108 }
109
Aaron Huang6763aec2019-10-02 23:37:02 +0800110 public IpConfiguration(@NonNull IpConfiguration source) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900111 this();
112 if (source != null) {
113 init(source.ipAssignment, source.proxySettings,
114 source.staticIpConfiguration, source.httpProxy);
115 }
116 }
117
Aaron Huang6763aec2019-10-02 23:37:02 +0800118 public @NonNull IpAssignment getIpAssignment() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900119 return ipAssignment;
120 }
121
Aaron Huang6763aec2019-10-02 23:37:02 +0800122 public void setIpAssignment(@NonNull IpAssignment ipAssignment) {
Jaewan Kim63461552014-03-10 17:10:51 +0900123 this.ipAssignment = ipAssignment;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900124 }
125
Aaron Huang6763aec2019-10-02 23:37:02 +0800126 public @Nullable StaticIpConfiguration getStaticIpConfiguration() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900127 return staticIpConfiguration;
128 }
129
Aaron Huang6763aec2019-10-02 23:37:02 +0800130 public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900131 this.staticIpConfiguration = staticIpConfiguration;
132 }
133
Aaron Huang6763aec2019-10-02 23:37:02 +0800134 public @NonNull ProxySettings getProxySettings() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900135 return proxySettings;
136 }
137
Aaron Huang6763aec2019-10-02 23:37:02 +0800138 public void setProxySettings(@NonNull ProxySettings proxySettings) {
Jaewan Kim63461552014-03-10 17:10:51 +0900139 this.proxySettings = proxySettings;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900140 }
141
Aaron Huang6763aec2019-10-02 23:37:02 +0800142 public @Nullable ProxyInfo getHttpProxy() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900143 return httpProxy;
144 }
145
Aaron Huang6763aec2019-10-02 23:37:02 +0800146 public void setHttpProxy(@Nullable ProxyInfo httpProxy) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900147 this.httpProxy = httpProxy;
Jaewan Kim63461552014-03-10 17:10:51 +0900148 }
149
150 @Override
151 public String toString() {
152 StringBuilder sbuf = new StringBuilder();
153 sbuf.append("IP assignment: " + ipAssignment.toString());
154 sbuf.append("\n");
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900155 if (staticIpConfiguration != null) {
156 sbuf.append("Static configuration: " + staticIpConfiguration.toString());
157 sbuf.append("\n");
158 }
Jaewan Kim63461552014-03-10 17:10:51 +0900159 sbuf.append("Proxy settings: " + proxySettings.toString());
160 sbuf.append("\n");
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900161 if (httpProxy != null) {
162 sbuf.append("HTTP proxy: " + httpProxy.toString());
163 sbuf.append("\n");
164 }
Jaewan Kim63461552014-03-10 17:10:51 +0900165
166 return sbuf.toString();
167 }
168
169 @Override
170 public boolean equals(Object o) {
171 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 &&
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900182 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
183 Objects.equals(this.httpProxy, other.httpProxy);
Jaewan Kim63461552014-03-10 17:10:51 +0900184 }
185
186 @Override
187 public int hashCode() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900188 return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
Jaewan Kim63461552014-03-10 17:10:51 +0900189 17 * ipAssignment.ordinal() +
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900190 47 * proxySettings.ordinal() +
191 83 * httpProxy.hashCode();
Jaewan Kim63461552014-03-10 17:10:51 +0900192 }
193
Aaron Huang42daaca2019-11-28 14:17:32 +0800194 /** Implement the Parcelable interface */
Jaewan Kim63461552014-03-10 17:10:51 +0900195 public int describeContents() {
196 return 0;
197 }
198
Aaron Huang42daaca2019-11-28 14:17:32 +0800199 /** Implement the Parcelable interface */
Aaron Huang6763aec2019-10-02 23:37:02 +0800200 public void writeToParcel(@NonNull Parcel dest, int flags) {
Jaewan Kim63461552014-03-10 17:10:51 +0900201 dest.writeString(ipAssignment.name());
202 dest.writeString(proxySettings.name());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900203 dest.writeParcelable(staticIpConfiguration, flags);
204 dest.writeParcelable(httpProxy, flags);
Jaewan Kim63461552014-03-10 17:10:51 +0900205 }
206
207 /** Implement the Parcelable interface */
Aaron Huang6763aec2019-10-02 23:37:02 +0800208 public static final @NonNull Creator<IpConfiguration> CREATOR =
Jaewan Kim63461552014-03-10 17:10:51 +0900209 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());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900214 config.staticIpConfiguration = in.readParcelable(null);
215 config.httpProxy = in.readParcelable(null);
Jaewan Kim63461552014-03-10 17:10:51 +0900216 return config;
217 }
218
219 public IpConfiguration[] newArray(int size) {
220 return new IpConfiguration[size];
221 }
222 };
223}