San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 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 | |
| 17 | package android.net; |
| 18 | |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 19 | import android.os.Parcel; |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 20 | import android.os.Parcelable; |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 21 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 22 | import com.google.android.collect.Sets; |
| 23 | |
| 24 | import java.util.HashSet; |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 25 | |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 26 | /** |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 27 | * Configuration details for a network interface. |
| 28 | * |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 29 | * @hide |
| 30 | */ |
| 31 | public class InterfaceConfiguration implements Parcelable { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 32 | private String mHwAddr; |
| 33 | private LinkAddress mAddr; |
| 34 | private HashSet<String> mFlags = Sets.newHashSet(); |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 35 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 36 | 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 Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 48 | public Iterable<String> getFlags() { |
| 49 | return mFlags; |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 50 | } |
| 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 | |
Christopher Wiley | ce5d913 | 2016-09-13 12:07:58 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Set flags so that no changes will be made to the up/down status. |
| 85 | */ |
| 86 | public void ignoreInterfaceUpDownStatus() { |
| 87 | mFlags.remove(FLAG_UP); |
| 88 | mFlags.remove(FLAG_DOWN); |
| 89 | } |
| 90 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 91 | public LinkAddress getLinkAddress() { |
| 92 | return mAddr; |
| 93 | } |
| 94 | |
| 95 | public void setLinkAddress(LinkAddress addr) { |
| 96 | mAddr = addr; |
| 97 | } |
| 98 | |
| 99 | public String getHardwareAddress() { |
| 100 | return mHwAddr; |
| 101 | } |
| 102 | |
| 103 | public void setHardwareAddress(String hwAddr) { |
| 104 | mHwAddr = hwAddr; |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Irfan Sheriff | 2955209 | 2011-01-17 12:38:30 -0800 | [diff] [blame] | 107 | /** |
| 108 | * This function determines if the interface is up and has a valid IP |
| 109 | * configuration (IP address has a non zero octet). |
| 110 | * |
| 111 | * Note: It is supposed to be quick and hence should not initiate |
| 112 | * any network activity |
| 113 | */ |
| 114 | public boolean isActive() { |
| 115 | try { |
Christopher Wiley | 87100cc | 2016-10-17 10:30:28 -0700 | [diff] [blame] | 116 | if (isUp()) { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 117 | for (byte b : mAddr.getAddress().getAddress()) { |
Irfan Sheriff | 2955209 | 2011-01-17 12:38:30 -0800 | [diff] [blame] | 118 | if (b != 0) return true; |
| 119 | } |
| 120 | } |
| 121 | } catch (NullPointerException e) { |
| 122 | return false; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
Christopher Wiley | 08395dc | 2016-10-10 09:24:03 -0700 | [diff] [blame] | 127 | public boolean isUp() { |
| 128 | return hasFlag(FLAG_UP); |
| 129 | } |
| 130 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 131 | /** {@inheritDoc} */ |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 132 | public int describeContents() { |
| 133 | return 0; |
| 134 | } |
| 135 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 136 | /** {@inheritDoc} */ |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 137 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 138 | dest.writeString(mHwAddr); |
| 139 | if (mAddr != null) { |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 140 | dest.writeByte((byte)1); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 141 | dest.writeParcelable(mAddr, flags); |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 142 | } else { |
| 143 | dest.writeByte((byte)0); |
| 144 | } |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 145 | dest.writeInt(mFlags.size()); |
| 146 | for (String flag : mFlags) { |
| 147 | dest.writeString(flag); |
| 148 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 151 | public static final Creator<InterfaceConfiguration> CREATOR = new Creator< |
| 152 | InterfaceConfiguration>() { |
| 153 | public InterfaceConfiguration createFromParcel(Parcel in) { |
| 154 | InterfaceConfiguration info = new InterfaceConfiguration(); |
| 155 | info.mHwAddr = in.readString(); |
| 156 | if (in.readByte() == 1) { |
| 157 | info.mAddr = in.readParcelable(null); |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 158 | } |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 159 | final int size = in.readInt(); |
| 160 | for (int i = 0; i < size; i++) { |
| 161 | info.mFlags.add(in.readString()); |
| 162 | } |
| 163 | return info; |
| 164 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 165 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 166 | public InterfaceConfiguration[] newArray(int size) { |
| 167 | return new InterfaceConfiguration[size]; |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | private static void validateFlag(String flag) { |
| 172 | if (flag.indexOf(' ') >= 0) { |
| 173 | throw new IllegalArgumentException("flag contains space: " + flag); |
| 174 | } |
| 175 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 176 | } |