blob: 3319f33878c1d7cda8d9e28534f1e2a39aae138b [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
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010019import android.annotation.UnsupportedAppUsage;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090020import android.net.StaticIpConfiguration;
Jaewan Kim63461552014-03-10 17:10:51 +090021import android.os.Parcel;
22import android.os.Parcelable;
23
24import java.util.Objects;
25
26/**
27 * A class representing a configured network.
28 * @hide
29 */
30public class IpConfiguration implements Parcelable {
31 private static final String TAG = "IpConfiguration";
32
33 public enum IpAssignment {
34 /* Use statically configured IP settings. Configuration can be accessed
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090035 * with staticIpConfiguration */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010036 @UnsupportedAppUsage
Jaewan Kim63461552014-03-10 17:10:51 +090037 STATIC,
Blake Lawson0c9ed962018-08-23 08:43:07 -070038 /* Use dynamically configured IP settings */
Jaewan Kim63461552014-03-10 17:10:51 +090039 DHCP,
40 /* no IP details are assigned, this is used to indicate
41 * that any existing IP settings should be retained */
42 UNASSIGNED
43 }
44
45 public IpAssignment ipAssignment;
46
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090047 public StaticIpConfiguration staticIpConfiguration;
48
Jaewan Kim63461552014-03-10 17:10:51 +090049 public enum ProxySettings {
50 /* No proxy is to be used. Any existing proxy settings
51 * should be cleared. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010052 @UnsupportedAppUsage
Jaewan Kim63461552014-03-10 17:10:51 +090053 NONE,
54 /* Use statically configured proxy. Configuration can be accessed
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090055 * with httpProxy. */
Jaewan Kim63461552014-03-10 17:10:51 +090056 STATIC,
57 /* no proxy details are assigned, this is used to indicate
58 * that any existing proxy settings should be retained */
59 UNASSIGNED,
60 /* Use a Pac based proxy.
61 */
62 PAC
63 }
64
65 public ProxySettings proxySettings;
66
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010067 @UnsupportedAppUsage
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090068 public ProxyInfo httpProxy;
Jaewan Kim63461552014-03-10 17:10:51 +090069
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090070 private void init(IpAssignment ipAssignment,
71 ProxySettings proxySettings,
72 StaticIpConfiguration staticIpConfiguration,
73 ProxyInfo httpProxy) {
74 this.ipAssignment = ipAssignment;
75 this.proxySettings = proxySettings;
76 this.staticIpConfiguration = (staticIpConfiguration == null) ?
77 null : new StaticIpConfiguration(staticIpConfiguration);
78 this.httpProxy = (httpProxy == null) ?
79 null : new ProxyInfo(httpProxy);
Jaewan Kim63461552014-03-10 17:10:51 +090080 }
81
82 public IpConfiguration() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090083 init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
Jaewan Kim63461552014-03-10 17:10:51 +090084 }
85
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010086 @UnsupportedAppUsage
Jaewan Kim63461552014-03-10 17:10:51 +090087 public IpConfiguration(IpAssignment ipAssignment,
88 ProxySettings proxySettings,
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090089 StaticIpConfiguration staticIpConfiguration,
90 ProxyInfo httpProxy) {
91 init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
92 }
93
94 public IpConfiguration(IpConfiguration source) {
95 this();
96 if (source != null) {
97 init(source.ipAssignment, source.proxySettings,
98 source.staticIpConfiguration, source.httpProxy);
99 }
100 }
101
102 public IpAssignment getIpAssignment() {
103 return ipAssignment;
104 }
105
106 public void setIpAssignment(IpAssignment ipAssignment) {
Jaewan Kim63461552014-03-10 17:10:51 +0900107 this.ipAssignment = ipAssignment;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900108 }
109
110 public StaticIpConfiguration getStaticIpConfiguration() {
111 return staticIpConfiguration;
112 }
113
114 public void setStaticIpConfiguration(StaticIpConfiguration staticIpConfiguration) {
115 this.staticIpConfiguration = staticIpConfiguration;
116 }
117
118 public ProxySettings getProxySettings() {
119 return proxySettings;
120 }
121
122 public void setProxySettings(ProxySettings proxySettings) {
Jaewan Kim63461552014-03-10 17:10:51 +0900123 this.proxySettings = proxySettings;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900124 }
125
126 public ProxyInfo getHttpProxy() {
127 return httpProxy;
128 }
129
130 public void setHttpProxy(ProxyInfo httpProxy) {
131 this.httpProxy = httpProxy;
Jaewan Kim63461552014-03-10 17:10:51 +0900132 }
133
134 @Override
135 public String toString() {
136 StringBuilder sbuf = new StringBuilder();
137 sbuf.append("IP assignment: " + ipAssignment.toString());
138 sbuf.append("\n");
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900139 if (staticIpConfiguration != null) {
140 sbuf.append("Static configuration: " + staticIpConfiguration.toString());
141 sbuf.append("\n");
142 }
Jaewan Kim63461552014-03-10 17:10:51 +0900143 sbuf.append("Proxy settings: " + proxySettings.toString());
144 sbuf.append("\n");
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900145 if (httpProxy != null) {
146 sbuf.append("HTTP proxy: " + httpProxy.toString());
147 sbuf.append("\n");
148 }
Jaewan Kim63461552014-03-10 17:10:51 +0900149
150 return sbuf.toString();
151 }
152
153 @Override
154 public boolean equals(Object o) {
155 if (o == this) {
156 return true;
157 }
158
159 if (!(o instanceof IpConfiguration)) {
160 return false;
161 }
162
163 IpConfiguration other = (IpConfiguration) o;
164 return this.ipAssignment == other.ipAssignment &&
165 this.proxySettings == other.proxySettings &&
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900166 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
167 Objects.equals(this.httpProxy, other.httpProxy);
Jaewan Kim63461552014-03-10 17:10:51 +0900168 }
169
170 @Override
171 public int hashCode() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900172 return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
Jaewan Kim63461552014-03-10 17:10:51 +0900173 17 * ipAssignment.ordinal() +
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900174 47 * proxySettings.ordinal() +
175 83 * httpProxy.hashCode();
Jaewan Kim63461552014-03-10 17:10:51 +0900176 }
177
178 /** Implement the Parcelable interface */
179 public int describeContents() {
180 return 0;
181 }
182
183 /** Implement the Parcelable interface */
184 public void writeToParcel(Parcel dest, int flags) {
185 dest.writeString(ipAssignment.name());
186 dest.writeString(proxySettings.name());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900187 dest.writeParcelable(staticIpConfiguration, flags);
188 dest.writeParcelable(httpProxy, flags);
Jaewan Kim63461552014-03-10 17:10:51 +0900189 }
190
191 /** Implement the Parcelable interface */
192 public static final Creator<IpConfiguration> CREATOR =
193 new Creator<IpConfiguration>() {
194 public IpConfiguration createFromParcel(Parcel in) {
195 IpConfiguration config = new IpConfiguration();
196 config.ipAssignment = IpAssignment.valueOf(in.readString());
197 config.proxySettings = ProxySettings.valueOf(in.readString());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900198 config.staticIpConfiguration = in.readParcelable(null);
199 config.httpProxy = in.readParcelable(null);
Jaewan Kim63461552014-03-10 17:10:51 +0900200 return config;
201 }
202
203 public IpConfiguration[] newArray(int size) {
204 return new IpConfiguration[size];
205 }
206 };
207}