2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.os.Parcel;
20 import android.os.Parcelable;
22 import com.google.android.collect.Sets;
24 import java.util.HashSet;
27 * Configuration details for a network interface.
31 public class InterfaceConfiguration implements Parcelable {
32 private String mHwAddr;
33 private LinkAddress mAddr;
34 private HashSet<String> mFlags = Sets.newHashSet();
36 private static final String FLAG_UP = "up";
37 private static final String FLAG_DOWN = "down";
40 public String toString() {
41 final StringBuilder builder = new StringBuilder();
42 builder.append("mHwAddr=").append(mHwAddr);
43 builder.append(" mAddr=").append(String.valueOf(mAddr));
44 builder.append(" mFlags=").append(getFlags());
45 return builder.toString();
48 public Iterable<String> getFlags() {
52 public boolean hasFlag(String flag) {
54 return mFlags.contains(flag);
57 public void clearFlag(String flag) {
62 public void setFlag(String flag) {
68 * Set flags to mark interface as up.
70 public void setInterfaceUp() {
71 mFlags.remove(FLAG_DOWN);
76 * Set flags to mark interface as down.
78 public void setInterfaceDown() {
79 mFlags.remove(FLAG_UP);
80 mFlags.add(FLAG_DOWN);
83 public LinkAddress getLinkAddress() {
87 public void setLinkAddress(LinkAddress addr) {
91 public String getHardwareAddress() {
95 public void setHardwareAddress(String hwAddr) {
100 * This function determines if the interface is up and has a valid IP
101 * configuration (IP address has a non zero octet).
103 * Note: It is supposed to be quick and hence should not initiate
104 * any network activity
106 public boolean isActive() {
108 if (hasFlag(FLAG_UP)) {
109 for (byte b : mAddr.getAddress().getAddress()) {
110 if (b != 0) return true;
113 } catch (NullPointerException e) {
120 public int describeContents() {
125 public void writeToParcel(Parcel dest, int flags) {
126 dest.writeString(mHwAddr);
128 dest.writeByte((byte)1);
129 dest.writeParcelable(mAddr, flags);
131 dest.writeByte((byte)0);
133 dest.writeInt(mFlags.size());
134 for (String flag : mFlags) {
135 dest.writeString(flag);
139 public static final Creator<InterfaceConfiguration> CREATOR = new Creator<
140 InterfaceConfiguration>() {
141 public InterfaceConfiguration createFromParcel(Parcel in) {
142 InterfaceConfiguration info = new InterfaceConfiguration();
143 info.mHwAddr = in.readString();
144 if (in.readByte() == 1) {
145 info.mAddr = in.readParcelable(null);
147 final int size = in.readInt();
148 for (int i = 0; i < size; i++) {
149 info.mFlags.add(in.readString());
154 public InterfaceConfiguration[] newArray(int size) {
155 return new InterfaceConfiguration[size];
159 private static void validateFlag(String flag) {
160 if (flag.indexOf(' ') >= 0) {
161 throw new IllegalArgumentException("flag contains space: " + flag);