1 /*
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 
17 package android.net;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SuppressLint;
22 import android.annotation.SystemApi;
23 import android.compat.annotation.UnsupportedAppUsage;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import java.util.Objects;
28 
29 /**
30  * A class representing a configured network.
31  * @hide
32  */
33 @SystemApi
34 public final class IpConfiguration implements Parcelable {
35     private static final String TAG = "IpConfiguration";
36 
37     // 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")
41     public enum IpAssignment {
42         /* Use statically configured IP settings. Configuration can be accessed
43          * with staticIpConfiguration */
44         STATIC,
45         /* Use dynamically configured IP settings */
46         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 
52     /** @hide */
53     public IpAssignment ipAssignment;
54 
55     /** @hide */
56     public StaticIpConfiguration staticIpConfiguration;
57 
58     // 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")
62     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
67          * with httpProxy. */
68         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 
77     /** @hide */
78     public ProxySettings proxySettings;
79 
80     /** @hide */
81     @UnsupportedAppUsage
82     public ProxyInfo httpProxy;
83 
init(IpAssignment ipAssignment, ProxySettings proxySettings, StaticIpConfiguration staticIpConfiguration, ProxyInfo httpProxy)84     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);
94     }
95 
IpConfiguration()96     public IpConfiguration() {
97         init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
98     }
99 
100     /** @hide */
101     @UnsupportedAppUsage
IpConfiguration(IpAssignment ipAssignment, ProxySettings proxySettings, StaticIpConfiguration staticIpConfiguration, ProxyInfo httpProxy)102     public IpConfiguration(IpAssignment ipAssignment,
103                            ProxySettings proxySettings,
104                            StaticIpConfiguration staticIpConfiguration,
105                            ProxyInfo httpProxy) {
106         init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
107     }
108 
IpConfiguration(@onNull IpConfiguration source)109     public IpConfiguration(@NonNull IpConfiguration source) {
110         this();
111         if (source != null) {
112             init(source.ipAssignment, source.proxySettings,
113                  source.staticIpConfiguration, source.httpProxy);
114         }
115     }
116 
getIpAssignment()117     public @NonNull IpAssignment getIpAssignment() {
118         return ipAssignment;
119     }
120 
setIpAssignment(@onNull IpAssignment ipAssignment)121     public void setIpAssignment(@NonNull IpAssignment ipAssignment) {
122         this.ipAssignment = ipAssignment;
123     }
124 
getStaticIpConfiguration()125     public @Nullable StaticIpConfiguration getStaticIpConfiguration() {
126         return staticIpConfiguration;
127     }
128 
setStaticIpConfiguration(@ullable StaticIpConfiguration staticIpConfiguration)129     public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) {
130         this.staticIpConfiguration = staticIpConfiguration;
131     }
132 
getProxySettings()133     public @NonNull ProxySettings getProxySettings() {
134         return proxySettings;
135     }
136 
setProxySettings(@onNull ProxySettings proxySettings)137     public void setProxySettings(@NonNull ProxySettings proxySettings) {
138         this.proxySettings = proxySettings;
139     }
140 
getHttpProxy()141     public @Nullable ProxyInfo getHttpProxy() {
142         return httpProxy;
143     }
144 
setHttpProxy(@ullable ProxyInfo httpProxy)145     public void setHttpProxy(@Nullable ProxyInfo httpProxy) {
146         this.httpProxy = httpProxy;
147     }
148 
149     @Override
toString()150     public String toString() {
151         StringBuilder sbuf = new StringBuilder();
152         sbuf.append("IP assignment: " + ipAssignment.toString());
153         sbuf.append("\n");
154         if (staticIpConfiguration != null) {
155             sbuf.append("Static configuration: " + staticIpConfiguration.toString());
156             sbuf.append("\n");
157         }
158         sbuf.append("Proxy settings: " + proxySettings.toString());
159         sbuf.append("\n");
160         if (httpProxy != null) {
161             sbuf.append("HTTP proxy: " + httpProxy.toString());
162             sbuf.append("\n");
163         }
164 
165         return sbuf.toString();
166     }
167 
168     @Override
equals(Object o)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 &&
181                 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
182                 Objects.equals(this.httpProxy, other.httpProxy);
183     }
184 
185     @Override
hashCode()186     public int hashCode() {
187         return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
188                17 * ipAssignment.ordinal() +
189                47 * proxySettings.ordinal() +
190                83 * httpProxy.hashCode();
191     }
192 
193     /** Implement the Parcelable interface */
describeContents()194     public int describeContents() {
195         return 0;
196     }
197 
198     /** Implement the Parcelable interface */
writeToParcel(@onNull Parcel dest, int flags)199     public void writeToParcel(@NonNull Parcel dest, int flags) {
200         dest.writeString(ipAssignment.name());
201         dest.writeString(proxySettings.name());
202         dest.writeParcelable(staticIpConfiguration, flags);
203         dest.writeParcelable(httpProxy, flags);
204     }
205 
206     /** Implement the Parcelable interface */
207     public static final @NonNull Creator<IpConfiguration> CREATOR =
208         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());
213                 config.staticIpConfiguration = in.readParcelable(null);
214                 config.httpProxy = in.readParcelable(null);
215                 return config;
216             }
217 
218             public IpConfiguration[] newArray(int size) {
219                 return new IpConfiguration[size];
220             }
221         };
222 }
223