DhcpInfo.java revision 31b62322bfa9470d648fbfd69510e03da29b29af
1/*
2 * Copyright (C) 2008 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.os.Parcelable;
20import android.os.Parcel;
21
22/**
23 * A simple object for retrieving the results of a DHCP request.
24 */
25public class DhcpInfo implements Parcelable {
26    public int ipAddress;
27    public int gateway;
28    public int netmask;
29
30    public int dns1;
31    public int dns2;
32
33    public int serverAddress;
34    public int leaseDuration;
35
36    public DhcpInfo() {
37        super();
38    }
39
40    /** copy constructor {@hide} */
41    public DhcpInfo(DhcpInfo source) {
42        if (source != null) {
43            ipAddress = source.ipAddress;
44            gateway = source.gateway;
45            netmask = source.netmask;
46            dns1 = source.dns1;
47            dns2 = source.dns2;
48            serverAddress = source.serverAddress;
49            leaseDuration = source.leaseDuration;
50        }
51    }
52
53    public String toString() {
54        StringBuffer str = new StringBuffer();
55
56        str.append("ipaddr "); putAddress(str, ipAddress);
57        str.append(" gateway "); putAddress(str, gateway);
58        str.append(" netmask "); putAddress(str, netmask);
59        str.append(" dns1 "); putAddress(str, dns1);
60        str.append(" dns2 "); putAddress(str, dns2);
61        str.append(" DHCP server "); putAddress(str, serverAddress);
62        str.append(" lease ").append(leaseDuration).append(" seconds");
63
64        return str.toString();
65    }
66
67    private static void putAddress(StringBuffer buf, int addr) {
68        buf.append(addr  & 0xff).append('.').
69            append((addr >>>= 8) & 0xff).append('.').
70            append((addr >>>= 8) & 0xff).append('.').
71            append((addr >>>= 8) & 0xff);
72    }
73
74    /** Implement the Parcelable interface {@hide} */
75    public int describeContents() {
76        return 0;
77    }
78
79    /** Implement the Parcelable interface {@hide} */
80    public void writeToParcel(Parcel dest, int flags) {
81        dest.writeInt(ipAddress);
82        dest.writeInt(gateway);
83        dest.writeInt(netmask);
84        dest.writeInt(dns1);
85        dest.writeInt(dns2);
86        dest.writeInt(serverAddress);
87        dest.writeInt(leaseDuration);
88    }
89
90    /** Implement the Parcelable interface {@hide} */
91    public static final Creator<DhcpInfo> CREATOR =
92        new Creator<DhcpInfo>() {
93            public DhcpInfo createFromParcel(Parcel in) {
94                DhcpInfo info = new DhcpInfo();
95                info.ipAddress = in.readInt();
96                info.gateway = in.readInt();
97                info.netmask = in.readInt();
98                info.dns1 = in.readInt();
99                info.dns2 = in.readInt();
100                info.serverAddress = in.readInt();
101                info.leaseDuration = in.readInt();
102                return info;
103            }
104
105            public DhcpInfo[] newArray(int size) {
106                return new DhcpInfo[size];
107            }
108        };
109}
110