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
17package android.net;
18
19import android.net.StaticIpConfiguration;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.Objects;
24
25/**
26 * A class representing a configured network.
27 * @hide
28 */
29public class IpConfiguration implements Parcelable {
30    private static final String TAG = "IpConfiguration";
31
32    public enum IpAssignment {
33        /* Use statically configured IP settings. Configuration can be accessed
34         * with staticIpConfiguration */
35        STATIC,
36        /* Use dynamically configured IP settigns */
37        DHCP,
38        /* no IP details are assigned, this is used to indicate
39         * that any existing IP settings should be retained */
40        UNASSIGNED
41    }
42
43    public IpAssignment ipAssignment;
44
45    public StaticIpConfiguration staticIpConfiguration;
46
47    public enum ProxySettings {
48        /* No proxy is to be used. Any existing proxy settings
49         * should be cleared. */
50        NONE,
51        /* Use statically configured proxy. Configuration can be accessed
52         * with httpProxy. */
53        STATIC,
54        /* no proxy details are assigned, this is used to indicate
55         * that any existing proxy settings should be retained */
56        UNASSIGNED,
57        /* Use a Pac based proxy.
58         */
59        PAC
60    }
61
62    public ProxySettings proxySettings;
63
64    public ProxyInfo httpProxy;
65
66    private void init(IpAssignment ipAssignment,
67                      ProxySettings proxySettings,
68                      StaticIpConfiguration staticIpConfiguration,
69                      ProxyInfo httpProxy) {
70        this.ipAssignment = ipAssignment;
71        this.proxySettings = proxySettings;
72        this.staticIpConfiguration = (staticIpConfiguration == null) ?
73                null : new StaticIpConfiguration(staticIpConfiguration);
74        this.httpProxy = (httpProxy == null) ?
75                null : new ProxyInfo(httpProxy);
76    }
77
78    public IpConfiguration() {
79        init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
80    }
81
82    public IpConfiguration(IpAssignment ipAssignment,
83                           ProxySettings proxySettings,
84                           StaticIpConfiguration staticIpConfiguration,
85                           ProxyInfo httpProxy) {
86        init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
87    }
88
89    public IpConfiguration(IpConfiguration source) {
90        this();
91        if (source != null) {
92            init(source.ipAssignment, source.proxySettings,
93                 source.staticIpConfiguration, source.httpProxy);
94        }
95    }
96
97    public IpAssignment getIpAssignment() {
98        return ipAssignment;
99    }
100
101    public void setIpAssignment(IpAssignment ipAssignment) {
102        this.ipAssignment = ipAssignment;
103    }
104
105    public StaticIpConfiguration getStaticIpConfiguration() {
106        return staticIpConfiguration;
107    }
108
109    public void setStaticIpConfiguration(StaticIpConfiguration staticIpConfiguration) {
110        this.staticIpConfiguration = staticIpConfiguration;
111    }
112
113    public ProxySettings getProxySettings() {
114        return proxySettings;
115    }
116
117    public void setProxySettings(ProxySettings proxySettings) {
118        this.proxySettings = proxySettings;
119    }
120
121    public ProxyInfo getHttpProxy() {
122        return httpProxy;
123    }
124
125    public void setHttpProxy(ProxyInfo httpProxy) {
126        this.httpProxy = httpProxy;
127    }
128
129    @Override
130    public String toString() {
131        StringBuilder sbuf = new StringBuilder();
132        sbuf.append("IP assignment: " + ipAssignment.toString());
133        sbuf.append("\n");
134        if (staticIpConfiguration != null) {
135            sbuf.append("Static configuration: " + staticIpConfiguration.toString());
136            sbuf.append("\n");
137        }
138        sbuf.append("Proxy settings: " + proxySettings.toString());
139        sbuf.append("\n");
140        if (httpProxy != null) {
141            sbuf.append("HTTP proxy: " + httpProxy.toString());
142            sbuf.append("\n");
143        }
144
145        return sbuf.toString();
146    }
147
148    @Override
149    public boolean equals(Object o) {
150        if (o == this) {
151            return true;
152        }
153
154        if (!(o instanceof IpConfiguration)) {
155            return false;
156        }
157
158        IpConfiguration other = (IpConfiguration) o;
159        return this.ipAssignment == other.ipAssignment &&
160                this.proxySettings == other.proxySettings &&
161                Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
162                Objects.equals(this.httpProxy, other.httpProxy);
163    }
164
165    @Override
166    public int hashCode() {
167        return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
168               17 * ipAssignment.ordinal() +
169               47 * proxySettings.ordinal() +
170               83 * httpProxy.hashCode();
171    }
172
173    /** Implement the Parcelable interface */
174    public int describeContents() {
175        return 0;
176    }
177
178    /** Implement the Parcelable interface  */
179    public void writeToParcel(Parcel dest, int flags) {
180        dest.writeString(ipAssignment.name());
181        dest.writeString(proxySettings.name());
182        dest.writeParcelable(staticIpConfiguration, flags);
183        dest.writeParcelable(httpProxy, flags);
184    }
185
186    /** Implement the Parcelable interface */
187    public static final Creator<IpConfiguration> CREATOR =
188        new Creator<IpConfiguration>() {
189            public IpConfiguration createFromParcel(Parcel in) {
190                IpConfiguration config = new IpConfiguration();
191                config.ipAssignment = IpAssignment.valueOf(in.readString());
192                config.proxySettings = ProxySettings.valueOf(in.readString());
193                config.staticIpConfiguration = in.readParcelable(null);
194                config.httpProxy = in.readParcelable(null);
195                return config;
196            }
197
198            public IpConfiguration[] newArray(int size) {
199                return new IpConfiguration[size];
200            }
201        };
202}
203