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.net.LinkProperties;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.Objects;
24 
25 /**
26  * A class representing a configured network.
27  * @hide
28  */
29 public 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 linkProperties */
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 enum ProxySettings {
46         /* No proxy is to be used. Any existing proxy settings
47          * should be cleared. */
48         NONE,
49         /* Use statically configured proxy. Configuration can be accessed
50          * with linkProperties */
51         STATIC,
52         /* no proxy details are assigned, this is used to indicate
53          * that any existing proxy settings should be retained */
54         UNASSIGNED,
55         /* Use a Pac based proxy.
56          */
57         PAC
58     }
59 
60     public ProxySettings proxySettings;
61 
62     public LinkProperties linkProperties;
63 
64     public IpConfiguration(IpConfiguration source) {
65         if (source != null) {
66             ipAssignment = source.ipAssignment;
67             proxySettings = source.proxySettings;
68             linkProperties = new LinkProperties(source.linkProperties);
69         } else {
70             ipAssignment = IpAssignment.UNASSIGNED;
71             proxySettings = ProxySettings.UNASSIGNED;
72             linkProperties = new LinkProperties();
73         }
74     }
75 
76     public IpConfiguration() {
77          this(null);
78     }
79 
80     public IpConfiguration(IpAssignment ipAssignment,
81                            ProxySettings proxySettings,
82                            LinkProperties linkProperties) {
83         this.ipAssignment = ipAssignment;
84         this.proxySettings = proxySettings;
85         this.linkProperties = new LinkProperties(linkProperties);
86     }
87 
88     @Override
89     public String toString() {
90         StringBuilder sbuf = new StringBuilder();
91         sbuf.append("IP assignment: " + ipAssignment.toString());
92         sbuf.append("\n");
93         sbuf.append("Proxy settings: " + proxySettings.toString());
94         sbuf.append("\n");
95         sbuf.append(linkProperties.toString());
96         sbuf.append("\n");
97 
98         return sbuf.toString();
99     }
100 
101     @Override
102     public boolean equals(Object o) {
103         if (o == this) {
104             return true;
105         }
106 
107         if (!(o instanceof IpConfiguration)) {
108             return false;
109         }
110 
111         IpConfiguration other = (IpConfiguration) o;
112         return this.ipAssignment == other.ipAssignment &&
113                 this.proxySettings == other.proxySettings &&
114                 Objects.equals(this.linkProperties, other.linkProperties);
115     }
116 
117     @Override
118     public int hashCode() {
119         return 13 + (linkProperties != null ? linkProperties.hashCode() : 0) +
120                17 * ipAssignment.ordinal() +
121                47 * proxySettings.ordinal();
122     }
123 
124     /** Implement the Parcelable interface */
125     public int describeContents() {
126         return 0;
127     }
128 
129     /** Implement the Parcelable interface  */
130     public void writeToParcel(Parcel dest, int flags) {
131         dest.writeString(ipAssignment.name());
132         dest.writeString(proxySettings.name());
133         dest.writeParcelable(linkProperties, flags);
134     }
135 
136     /** Implement the Parcelable interface */
137     public static final Creator<IpConfiguration> CREATOR =
138         new Creator<IpConfiguration>() {
139             public IpConfiguration createFromParcel(Parcel in) {
140                 IpConfiguration config = new IpConfiguration();
141                 config.ipAssignment = IpAssignment.valueOf(in.readString());
142                 config.proxySettings = ProxySettings.valueOf(in.readString());
143                 config.linkProperties = in.readParcelable(null);
144                 return config;
145             }
146 
147             public IpConfiguration[] newArray(int size) {
148                 return new IpConfiguration[size];
149             }
150         };
151 }
152