blob: 8cdd153050183e67da2e6f7fb9c6e878e35642c0 [file] [log] [blame]
San Mehat353ced72010-01-22 12:22:54 -08001/*
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
San Mehat353ced72010-01-22 12:22:54 -080019import android.os.Parcel;
Jeff Sharkeyddba1062011-11-29 18:37:04 -080020import android.os.Parcelable;
San Mehat353ced72010-01-22 12:22:54 -080021
Jeff Sharkeyddba1062011-11-29 18:37:04 -080022import com.google.android.collect.Sets;
23
24import java.util.HashSet;
Robert Greenwalt04808c22010-12-13 17:01:41 -080025
San Mehat353ced72010-01-22 12:22:54 -080026/**
Jeff Sharkeyddba1062011-11-29 18:37:04 -080027 * Configuration details for a network interface.
28 *
San Mehat353ced72010-01-22 12:22:54 -080029 * @hide
30 */
31public class InterfaceConfiguration implements Parcelable {
Jeff Sharkeyddba1062011-11-29 18:37:04 -080032 private String mHwAddr;
33 private LinkAddress mAddr;
34 private HashSet<String> mFlags = Sets.newHashSet();
San Mehat353ced72010-01-22 12:22:54 -080035
Jeff Sharkeyddba1062011-11-29 18:37:04 -080036 private static final String FLAG_UP = "up";
37 private static final String FLAG_DOWN = "down";
38
39 @Override
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();
San Mehat353ced72010-01-22 12:22:54 -080046 }
47
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080048 public Iterable<String> getFlags() {
49 return mFlags;
Jeff Sharkeyddba1062011-11-29 18:37:04 -080050 }
51
52 public boolean hasFlag(String flag) {
53 validateFlag(flag);
54 return mFlags.contains(flag);
55 }
56
57 public void clearFlag(String flag) {
58 validateFlag(flag);
59 mFlags.remove(flag);
60 }
61
62 public void setFlag(String flag) {
63 validateFlag(flag);
64 mFlags.add(flag);
65 }
66
67 /**
68 * Set flags to mark interface as up.
69 */
70 public void setInterfaceUp() {
71 mFlags.remove(FLAG_DOWN);
72 mFlags.add(FLAG_UP);
73 }
74
75 /**
76 * Set flags to mark interface as down.
77 */
78 public void setInterfaceDown() {
79 mFlags.remove(FLAG_UP);
80 mFlags.add(FLAG_DOWN);
81 }
82
83 public LinkAddress getLinkAddress() {
84 return mAddr;
85 }
86
87 public void setLinkAddress(LinkAddress addr) {
88 mAddr = addr;
89 }
90
91 public String getHardwareAddress() {
92 return mHwAddr;
93 }
94
95 public void setHardwareAddress(String hwAddr) {
96 mHwAddr = hwAddr;
San Mehat353ced72010-01-22 12:22:54 -080097 }
98
Irfan Sheriff29552092011-01-17 12:38:30 -080099 /**
100 * This function determines if the interface is up and has a valid IP
101 * configuration (IP address has a non zero octet).
102 *
103 * Note: It is supposed to be quick and hence should not initiate
104 * any network activity
105 */
106 public boolean isActive() {
107 try {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800108 if (hasFlag(FLAG_UP)) {
109 for (byte b : mAddr.getAddress().getAddress()) {
Irfan Sheriff29552092011-01-17 12:38:30 -0800110 if (b != 0) return true;
111 }
112 }
113 } catch (NullPointerException e) {
114 return false;
115 }
116 return false;
117 }
118
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800119 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800120 public int describeContents() {
121 return 0;
122 }
123
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800124 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800125 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800126 dest.writeString(mHwAddr);
127 if (mAddr != null) {
Robert Greenwalt04808c22010-12-13 17:01:41 -0800128 dest.writeByte((byte)1);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800129 dest.writeParcelable(mAddr, flags);
Robert Greenwalt04808c22010-12-13 17:01:41 -0800130 } else {
131 dest.writeByte((byte)0);
132 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800133 dest.writeInt(mFlags.size());
134 for (String flag : mFlags) {
135 dest.writeString(flag);
136 }
San Mehat353ced72010-01-22 12:22:54 -0800137 }
138
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800139 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);
San Mehat353ced72010-01-22 12:22:54 -0800146 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800147 final int size = in.readInt();
148 for (int i = 0; i < size; i++) {
149 info.mFlags.add(in.readString());
150 }
151 return info;
152 }
San Mehat353ced72010-01-22 12:22:54 -0800153
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800154 public InterfaceConfiguration[] newArray(int size) {
155 return new InterfaceConfiguration[size];
156 }
157 };
158
159 private static void validateFlag(String flag) {
160 if (flag.indexOf(' ') >= 0) {
161 throw new IllegalArgumentException("flag contains space: " + flag);
162 }
163 }
San Mehat353ced72010-01-22 12:22:54 -0800164}