Home | History | Annotate | Download | only in net
      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.UnsupportedAppUsage;
     20 import android.net.StaticIpConfiguration;
     21 import android.os.Parcel;
     22 import android.os.Parcelable;
     23 
     24 import java.util.Objects;
     25 
     26 /**
     27  * A class representing a configured network.
     28  * @hide
     29  */
     30 public 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
     35          * with staticIpConfiguration */
     36         @UnsupportedAppUsage
     37         STATIC,
     38         /* Use dynamically configured IP settings */
     39         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 
     47     public StaticIpConfiguration staticIpConfiguration;
     48 
     49     public enum ProxySettings {
     50         /* No proxy is to be used. Any existing proxy settings
     51          * should be cleared. */
     52         @UnsupportedAppUsage
     53         NONE,
     54         /* Use statically configured proxy. Configuration can be accessed
     55          * with httpProxy. */
     56         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 
     67     @UnsupportedAppUsage
     68     public ProxyInfo httpProxy;
     69 
     70     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);
     80     }
     81 
     82     public IpConfiguration() {
     83         init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
     84     }
     85 
     86     @UnsupportedAppUsage
     87     public IpConfiguration(IpAssignment ipAssignment,
     88                            ProxySettings proxySettings,
     89                            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) {
    107         this.ipAssignment = ipAssignment;
    108     }
    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) {
    123         this.proxySettings = proxySettings;
    124     }
    125 
    126     public ProxyInfo getHttpProxy() {
    127         return httpProxy;
    128     }
    129 
    130     public void setHttpProxy(ProxyInfo httpProxy) {
    131         this.httpProxy = httpProxy;
    132     }
    133 
    134     @Override
    135     public String toString() {
    136         StringBuilder sbuf = new StringBuilder();
    137         sbuf.append("IP assignment: " + ipAssignment.toString());
    138         sbuf.append("\n");
    139         if (staticIpConfiguration != null) {
    140             sbuf.append("Static configuration: " + staticIpConfiguration.toString());
    141             sbuf.append("\n");
    142         }
    143         sbuf.append("Proxy settings: " + proxySettings.toString());
    144         sbuf.append("\n");
    145         if (httpProxy != null) {
    146             sbuf.append("HTTP proxy: " + httpProxy.toString());
    147             sbuf.append("\n");
    148         }
    149 
    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 &&
    166                 Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) &&
    167                 Objects.equals(this.httpProxy, other.httpProxy);
    168     }
    169 
    170     @Override
    171     public int hashCode() {
    172         return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) +
    173                17 * ipAssignment.ordinal() +
    174                47 * proxySettings.ordinal() +
    175                83 * httpProxy.hashCode();
    176     }
    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());
    187         dest.writeParcelable(staticIpConfiguration, flags);
    188         dest.writeParcelable(httpProxy, flags);
    189     }
    190 
    191     /** Implement the Parcelable interface */
    192     public static final @android.annotation.NonNull 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());
    198                 config.staticIpConfiguration = in.readParcelable(null);
    199                 config.httpProxy = in.readParcelable(null);
    200                 return config;
    201             }
    202 
    203             public IpConfiguration[] newArray(int size) {
    204                 return new IpConfiguration[size];
    205             }
    206         };
    207 }
    208