1 /* 2 * Copyright (C) 2020 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 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.util.ArrayList; 26 import java.util.Collection; 27 import java.util.LinkedHashSet; 28 import java.util.List; 29 import java.util.Objects; 30 31 /** 32 * Information on a tethered downstream client. 33 * @hide 34 */ 35 @SystemApi 36 public final class TetheredClient implements Parcelable { 37 @NonNull 38 private final MacAddress mMacAddress; 39 @NonNull 40 private final List<AddressInfo> mAddresses; 41 // TODO: use an @IntDef here 42 private final int mTetheringType; 43 TetheredClient(@onNull MacAddress macAddress, @NonNull Collection<AddressInfo> addresses, int tetheringType)44 public TetheredClient(@NonNull MacAddress macAddress, 45 @NonNull Collection<AddressInfo> addresses, int tetheringType) { 46 mMacAddress = macAddress; 47 mAddresses = new ArrayList<>(addresses); 48 mTetheringType = tetheringType; 49 } 50 TetheredClient(@onNull Parcel in)51 private TetheredClient(@NonNull Parcel in) { 52 this(in.readParcelable(null), in.createTypedArrayList(AddressInfo.CREATOR), in.readInt()); 53 } 54 55 @Override writeToParcel(@onNull Parcel dest, int flags)56 public void writeToParcel(@NonNull Parcel dest, int flags) { 57 dest.writeParcelable(mMacAddress, flags); 58 dest.writeTypedList(mAddresses); 59 dest.writeInt(mTetheringType); 60 } 61 62 /** 63 * Get the MAC address used to identify the client. 64 */ 65 @NonNull getMacAddress()66 public MacAddress getMacAddress() { 67 return mMacAddress; 68 } 69 70 /** 71 * Get information on the list of addresses that are associated with the client. 72 */ 73 @NonNull getAddresses()74 public List<AddressInfo> getAddresses() { 75 return new ArrayList<>(mAddresses); 76 } 77 78 /** 79 * Get the type of tethering used by the client. 80 * @return one of the {@code TetheringManager#TETHERING_*} constants. 81 */ getTetheringType()82 public int getTetheringType() { 83 return mTetheringType; 84 } 85 86 /** 87 * Return a new {@link TetheredClient} that has all the attributes of this instance, plus the 88 * {@link AddressInfo} of the provided {@link TetheredClient}. 89 * 90 * <p>Duplicate addresses are removed. 91 * @hide 92 */ addAddresses(@onNull TetheredClient other)93 public TetheredClient addAddresses(@NonNull TetheredClient other) { 94 final LinkedHashSet<AddressInfo> newAddresses = new LinkedHashSet<>( 95 mAddresses.size() + other.mAddresses.size()); 96 newAddresses.addAll(mAddresses); 97 newAddresses.addAll(other.mAddresses); 98 return new TetheredClient(mMacAddress, newAddresses, mTetheringType); 99 } 100 101 @Override hashCode()102 public int hashCode() { 103 return Objects.hash(mMacAddress, mAddresses, mTetheringType); 104 } 105 106 @Override equals(@ullable Object obj)107 public boolean equals(@Nullable Object obj) { 108 if (!(obj instanceof TetheredClient)) return false; 109 final TetheredClient other = (TetheredClient) obj; 110 return mMacAddress.equals(other.mMacAddress) 111 && mAddresses.equals(other.mAddresses) 112 && mTetheringType == other.mTetheringType; 113 } 114 115 /** 116 * Information on an lease assigned to a tethered client. 117 */ 118 public static final class AddressInfo implements Parcelable { 119 @NonNull 120 private final LinkAddress mAddress; 121 @Nullable 122 private final String mHostname; 123 124 /** @hide */ AddressInfo(@onNull LinkAddress address, @Nullable String hostname)125 public AddressInfo(@NonNull LinkAddress address, @Nullable String hostname) { 126 this.mAddress = address; 127 this.mHostname = hostname; 128 } 129 AddressInfo(Parcel in)130 private AddressInfo(Parcel in) { 131 this(in.readParcelable(null), in.readString()); 132 } 133 134 @Override writeToParcel(@onNull Parcel dest, int flags)135 public void writeToParcel(@NonNull Parcel dest, int flags) { 136 dest.writeParcelable(mAddress, flags); 137 dest.writeString(mHostname); 138 } 139 140 /** 141 * Get the link address (including prefix length and lifetime) used by the client. 142 * 143 * This may be an IPv4 or IPv6 address. 144 */ 145 @NonNull getAddress()146 public LinkAddress getAddress() { 147 return mAddress; 148 } 149 150 /** 151 * Get the hostname that was advertised by the client when obtaining its address, if any. 152 */ 153 @Nullable getHostname()154 public String getHostname() { 155 return mHostname; 156 } 157 158 /** 159 * Get the expiration time of the address assigned to the client. 160 * @hide 161 */ getExpirationTime()162 public long getExpirationTime() { 163 return mAddress.getExpirationTime(); 164 } 165 166 @Override describeContents()167 public int describeContents() { 168 return 0; 169 } 170 171 @Override hashCode()172 public int hashCode() { 173 return Objects.hash(mAddress, mHostname); 174 } 175 176 @Override equals(@ullable Object obj)177 public boolean equals(@Nullable Object obj) { 178 if (!(obj instanceof AddressInfo)) return false; 179 final AddressInfo other = (AddressInfo) obj; 180 // Use .equals() for addresses as all changes, including address expiry changes, 181 // should be included. 182 return other.mAddress.equals(mAddress) 183 && Objects.equals(mHostname, other.mHostname); 184 } 185 186 @NonNull 187 public static final Creator<AddressInfo> CREATOR = new Creator<AddressInfo>() { 188 @NonNull 189 @Override 190 public AddressInfo createFromParcel(@NonNull Parcel in) { 191 return new AddressInfo(in); 192 } 193 194 @NonNull 195 @Override 196 public AddressInfo[] newArray(int size) { 197 return new AddressInfo[size]; 198 } 199 }; 200 201 @NonNull 202 @Override toString()203 public String toString() { 204 return "AddressInfo {" 205 + mAddress 206 + (mHostname != null ? ", hostname " + mHostname : "") 207 + "}"; 208 } 209 } 210 211 @Override describeContents()212 public int describeContents() { 213 return 0; 214 } 215 216 @NonNull 217 public static final Creator<TetheredClient> CREATOR = new Creator<TetheredClient>() { 218 @NonNull 219 @Override 220 public TetheredClient createFromParcel(@NonNull Parcel in) { 221 return new TetheredClient(in); 222 } 223 224 @NonNull 225 @Override 226 public TetheredClient[] newArray(int size) { 227 return new TetheredClient[size]; 228 } 229 }; 230 231 @NonNull 232 @Override toString()233 public String toString() { 234 return "TetheredClient {hwAddr " + mMacAddress 235 + ", addresses " + mAddresses 236 + ", tetheringType " + mTetheringType 237 + "}"; 238 } 239 } 240