blob: 23d5ff7f3afe50d1c5c3b047e8ba87a697e44547 [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;
Artur Satayev26958002019-12-10 17:47:52 +000023import android.compat.annotation.UnsupportedAppUsage;
Jaewan Kim63461552014-03-10 17:10:51 +090024import android.os.Parcel;
25import android.os.Parcelable;
26
27import java.util.Objects;
28
29/**
30 * A class representing a configured network.
31 * @hide
32 */
Aaron Huang6763aec2019-10-02 23:37:02 +080033@SystemApi
34public final class IpConfiguration implements Parcelable {
Jaewan Kim63461552014-03-10 17:10:51 +090035 private static final String TAG = "IpConfiguration";
36
Aaron Huang6763aec2019-10-02 23:37:02 +080037 // This enum has been used by apps through reflection for many releases.
38 // Therefore they can't just be removed. Duplicating these constants to
39 // give an alternate SystemApi is a worse option than exposing them.
40 @SuppressLint("Enum")
Jaewan Kim63461552014-03-10 17:10:51 +090041 public enum IpAssignment {
42 /* Use statically configured IP settings. Configuration can be accessed
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090043 * with staticIpConfiguration */
Jaewan Kim63461552014-03-10 17:10:51 +090044 STATIC,
Blake Lawson0c9ed962018-08-23 08:43:07 -070045 /* Use dynamically configured IP settings */
Jaewan Kim63461552014-03-10 17:10:51 +090046 DHCP,
47 /* no IP details are assigned, this is used to indicate
48 * that any existing IP settings should be retained */
49 UNASSIGNED
50 }
51
Aaron Huang6763aec2019-10-02 23:37:02 +080052 /** @hide */
Jaewan Kim63461552014-03-10 17:10:51 +090053 public IpAssignment ipAssignment;
54
Aaron Huang6763aec2019-10-02 23:37:02 +080055 /** @hide */
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090056 public StaticIpConfiguration staticIpConfiguration;
57
Aaron Huang6763aec2019-10-02 23:37:02 +080058 // This enum has been used by apps through reflection for many releases.
59 // Therefore they can't just be removed. Duplicating these constants to
60 // give an alternate SystemApi is a worse option than exposing them.
61 @SuppressLint("Enum")
Jaewan Kim63461552014-03-10 17:10:51 +090062 public enum ProxySettings {
63 /* No proxy is to be used. Any existing proxy settings
64 * should be cleared. */
65 NONE,
66 /* Use statically configured proxy. Configuration can be accessed
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090067 * with httpProxy. */
Jaewan Kim63461552014-03-10 17:10:51 +090068 STATIC,
69 /* no proxy details are assigned, this is used to indicate
70 * that any existing proxy settings should be retained */
71 UNASSIGNED,
72 /* Use a Pac based proxy.
73 */
74 PAC
75 }
76
Aaron Huang6763aec2019-10-02 23:37:02 +080077 /** @hide */
Jaewan Kim63461552014-03-10 17:10:51 +090078 public ProxySettings proxySettings;
79
Aaron Huang6763aec2019-10-02 23:37:02 +080080 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010081 @UnsupportedAppUsage
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090082 public ProxyInfo httpProxy;
Jaewan Kim63461552014-03-10 17:10:51 +090083
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090084 private void init(IpAssignment ipAssignment,
85 ProxySettings proxySettings,
86 StaticIpConfiguration staticIpConfiguration,
87 ProxyInfo httpProxy) {
88 this.ipAssignment = ipAssignment;
89 this.proxySettings = proxySettings;
90 this.staticIpConfiguration = (staticIpConfiguration == null) ?
91 null : new StaticIpConfiguration(staticIpConfiguration);
92 this.httpProxy = (httpProxy == null) ?
93 null : new ProxyInfo(httpProxy);
Jaewan Kim63461552014-03-10 17:10:51 +090094 }
95
96 public IpConfiguration() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090097 init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
Jaewan Kim63461552014-03-10 17:10:51 +090098 }
99
Aaron Huang6763aec2019-10-02 23:37:02 +0800100 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100101 @UnsupportedAppUsage
Jaewan Kim63461552014-03-10 17:10:51 +0900102 public IpConfiguration(IpAssignment ipAssignment,
103 ProxySettings proxySettings,
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900104 StaticIpConfiguration staticIpConfiguration,
105 ProxyInfo httpProxy) {
106 init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
107 }
108
Aaron Huang6763aec2019-10-02 23:37:02 +0800109 public IpConfiguration(@NonNull IpConfiguration source) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900110 this();
111 if (source != null) {
112 init(source.ipAssignment, source.proxySettings,
113 source.staticIpConfiguration, source.httpProxy);
114 }
115 }
116
Aaron Huang6763aec2019-10-02 23:37:02 +0800117 public @NonNull IpAssignment getIpAssignment() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900118 return ipAssignment;
119 }
120
Aaron Huang6763aec2019-10-02 23:37:02 +0800121 public void setIpAssignment(@NonNull IpAssignment ipAssignment) {
Jaewan Kim63461552014-03-10 17:10:51 +0900122 this.ipAssignment = ipAssignment;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900123 }
124
Aaron Huang6763aec2019-10-02 23:37:02 +0800125 public @Nullable StaticIpConfiguration getStaticIpConfiguration() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900126 return staticIpConfiguration;
127 }
128
Aaron Huang6763aec2019-10-02 23:37:02 +0800129 public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900130 this.staticIpConfiguration = staticIpConfiguration;
131 }
132
Aaron Huang6763aec2019-10-02 23:37:02 +0800133 public @NonNull ProxySettings getProxySettings() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900134 return proxySettings;
135 }
136
Aaron Huang6763aec2019-10-02 23:37:02 +0800137 public void setProxySettings(@NonNull ProxySettings proxySettings) {
Jaewan Kim63461552014-03-10 17:10:51 +0900138 this.proxySettings = proxySettings;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900139 }
140
Aaron Huang6763aec2019-10-02 23:37:02 +0800141 public @Nullable ProxyInfo getHttpProxy() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900142 return httpProxy;
143 }
144
Aaron Huang6763aec2019-10-02 23:37:02 +0800145 public void setHttpProxy(@Nullable ProxyInfo httpProxy) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900146 this.httpProxy = httpProxy;
Jaewan Kim63461552014-03-10 17:10:51 +0900147 }
148
149 @Override
150 public String toString() {
151 StringBuilder sbuf = new StringBuilder();
152 sbuf.append("IP assignment: " + ipAssignment.toString());
153 sbuf.append("\n");
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900154 if (staticIpConfiguration != null) {
155 sbuf.append("Static configuration: " + staticIpConfiguration.toString());
156 sbuf.append("\n");
157 }
Jaewan Kim63461552014-03-10 17:10:51 +0900158 sbuf.append("Proxy settings: " + proxySettings.toString());
159 sbuf.append("\n");
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900160 if (httpProxy != null) {
161 sbuf.append("HTTP proxy: " + httpProxy.toString());
162 sbuf.append("\n");
163 }
Jaewan Kim63461552014-03-10 17:10:51 +0900164
165 return sbuf.toString();
166 }
167
168 @Override
169 public boolean equals(Object o) {
170 if (o == this) {
171 return true;
172 }
173
174 if (!(o instanceof IpConfiguration)) {
175 return false;
176 }
177
178 IpConfiguration other = (IpConfiguration) o;
179 return this.ipAssignment == other.ipAssignment &&
180 this.proxySettings == other.proxySettings &&
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900181 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
182 Objects.equals(this.httpProxy, other.httpProxy);
Jaewan Kim63461552014-03-10 17:10:51 +0900183 }
184
185 @Override
186 public int hashCode() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900187 return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
Jaewan Kim63461552014-03-10 17:10:51 +0900188 17 * ipAssignment.ordinal() +
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900189 47 * proxySettings.ordinal() +
190 83 * httpProxy.hashCode();
Jaewan Kim63461552014-03-10 17:10:51 +0900191 }
192
Aaron Huang42daaca2019-11-28 14:17:32 +0800193 /** Implement the Parcelable interface */
Jaewan Kim63461552014-03-10 17:10:51 +0900194 public int describeContents() {
195 return 0;
196 }
197
Aaron Huang42daaca2019-11-28 14:17:32 +0800198 /** Implement the Parcelable interface */
Aaron Huang6763aec2019-10-02 23:37:02 +0800199 public void writeToParcel(@NonNull Parcel dest, int flags) {
Jaewan Kim63461552014-03-10 17:10:51 +0900200 dest.writeString(ipAssignment.name());
201 dest.writeString(proxySettings.name());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900202 dest.writeParcelable(staticIpConfiguration, flags);
203 dest.writeParcelable(httpProxy, flags);
Jaewan Kim63461552014-03-10 17:10:51 +0900204 }
205
206 /** Implement the Parcelable interface */
Aaron Huang6763aec2019-10-02 23:37:02 +0800207 public static final @NonNull Creator<IpConfiguration> CREATOR =
Jaewan Kim63461552014-03-10 17:10:51 +0900208 new Creator<IpConfiguration>() {
209 public IpConfiguration createFromParcel(Parcel in) {
210 IpConfiguration config = new IpConfiguration();
211 config.ipAssignment = IpAssignment.valueOf(in.readString());
212 config.proxySettings = ProxySettings.valueOf(in.readString());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900213 config.staticIpConfiguration = in.readParcelable(null);
214 config.httpProxy = in.readParcelable(null);
Jaewan Kim63461552014-03-10 17:10:51 +0900215 return config;
216 }
217
218 public IpConfiguration[] newArray(int size) {
219 return new IpConfiguration[size];
220 }
221 };
222}