InterfaceConfiguration.java revision ed1264093234b1f1354cd5f669eb82a50bb56869
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
22import java.net.InetAddress;
23import java.net.UnknownHostException;
24
25/**
26 * A simple object for retrieving / setting an interfaces configuration
27 * @hide
28 */
29public class InterfaceConfiguration implements Parcelable {
30    public String hwAddr;
31    public LinkAddress addr;
32    public String interfaceFlags;
33
34    public InterfaceConfiguration() {
35        super();
36    }
37
38    public String toString() {
39        StringBuffer str = new StringBuffer();
40
41        str.append("ipddress ");
42        str.append((addr != null) ? addr.toString() : "NULL");
43        str.append(" flags ").append(interfaceFlags);
44        str.append(" hwaddr ").append(hwAddr);
45
46        return str.toString();
47    }
48
49    /**
50     * This function determines if the interface is up and has a valid IP
51     * configuration (IP address has a non zero octet).
52     *
53     * Note: It is supposed to be quick and hence should not initiate
54     * any network activity
55     */
56    public boolean isActive() {
57        try {
58            if(interfaceFlags.contains("up")) {
59                for (byte b : addr.getAddress().getAddress()) {
60                    if (b != 0) return true;
61                }
62            }
63        } catch (NullPointerException e) {
64            return false;
65        }
66        return false;
67    }
68
69    /** Implement the Parcelable interface {@hide} */
70    public int describeContents() {
71        return 0;
72    }
73
74    /** Implement the Parcelable interface {@hide} */
75    public void writeToParcel(Parcel dest, int flags) {
76        dest.writeString(hwAddr);
77        if (addr != null) {
78            dest.writeByte((byte)1);
79            dest.writeParcelable(addr, flags);
80        } else {
81            dest.writeByte((byte)0);
82        }
83        dest.writeString(interfaceFlags);
84    }
85
86    /** Implement the Parcelable interface {@hide} */
87    public static final Creator<InterfaceConfiguration> CREATOR =
88        new Creator<InterfaceConfiguration>() {
89            public InterfaceConfiguration createFromParcel(Parcel in) {
90                InterfaceConfiguration info = new InterfaceConfiguration();
91                info.hwAddr = in.readString();
92                if (in.readByte() == 1) {
93                    info.addr = in.readParcelable(null);
94                }
95                info.interfaceFlags = in.readString();
96                return info;
97            }
98
99            public InterfaceConfiguration[] newArray(int size) {
100                return new InterfaceConfiguration[size];
101            }
102        };
103}
104