1 /* 2 * Copyright (C) 2021 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.Objects; 26 27 /** 28 * Represents a request to update an existing Ethernet interface. 29 * 30 * @see EthernetManager#updateConfiguration 31 * 32 * @hide 33 */ 34 @SystemApi 35 public final class EthernetNetworkUpdateRequest implements Parcelable { 36 @Nullable 37 private final IpConfiguration mIpConfig; 38 @Nullable 39 private final NetworkCapabilities mNetworkCapabilities; 40 41 /** 42 * Setting the {@link IpConfiguration} is optional in {@link EthernetNetworkUpdateRequest}. 43 * When set to null, the existing IpConfiguration is not updated. 44 * 45 * @return the new {@link IpConfiguration} or null. 46 */ 47 @Nullable getIpConfiguration()48 public IpConfiguration getIpConfiguration() { 49 return mIpConfig == null ? null : new IpConfiguration(mIpConfig); 50 } 51 52 /** 53 * Setting the {@link NetworkCapabilities} is optional in {@link EthernetNetworkUpdateRequest}. 54 * When set to null, the existing NetworkCapabilities are not updated. 55 * 56 * @return the new {@link NetworkCapabilities} or null. 57 */ 58 @Nullable getNetworkCapabilities()59 public NetworkCapabilities getNetworkCapabilities() { 60 return mNetworkCapabilities == null ? null : new NetworkCapabilities(mNetworkCapabilities); 61 } 62 EthernetNetworkUpdateRequest(@ullable final IpConfiguration ipConfig, @Nullable final NetworkCapabilities networkCapabilities)63 private EthernetNetworkUpdateRequest(@Nullable final IpConfiguration ipConfig, 64 @Nullable final NetworkCapabilities networkCapabilities) { 65 mIpConfig = ipConfig; 66 mNetworkCapabilities = networkCapabilities; 67 } 68 EthernetNetworkUpdateRequest(@onNull final Parcel source)69 private EthernetNetworkUpdateRequest(@NonNull final Parcel source) { 70 Objects.requireNonNull(source); 71 mIpConfig = source.readParcelable(IpConfiguration.class.getClassLoader(), 72 IpConfiguration.class); 73 mNetworkCapabilities = source.readParcelable(NetworkCapabilities.class.getClassLoader(), 74 NetworkCapabilities.class); 75 } 76 77 /** 78 * Builder used to create {@link EthernetNetworkUpdateRequest} objects. 79 */ 80 public static final class Builder { 81 @Nullable 82 private IpConfiguration mBuilderIpConfig; 83 @Nullable 84 private NetworkCapabilities mBuilderNetworkCapabilities; 85 Builder()86 public Builder(){} 87 88 /** 89 * Constructor to populate the builder's values with an already built 90 * {@link EthernetNetworkUpdateRequest}. 91 * @param request the {@link EthernetNetworkUpdateRequest} to populate with. 92 */ Builder(@onNull final EthernetNetworkUpdateRequest request)93 public Builder(@NonNull final EthernetNetworkUpdateRequest request) { 94 Objects.requireNonNull(request); 95 mBuilderIpConfig = null == request.mIpConfig 96 ? null : new IpConfiguration(request.mIpConfig); 97 mBuilderNetworkCapabilities = null == request.mNetworkCapabilities 98 ? null : new NetworkCapabilities(request.mNetworkCapabilities); 99 } 100 101 /** 102 * Set the {@link IpConfiguration} to be used with the {@code Builder}. 103 * @param ipConfig the {@link IpConfiguration} to set. 104 * @return The builder to facilitate chaining. 105 */ 106 @NonNull setIpConfiguration(@ullable final IpConfiguration ipConfig)107 public Builder setIpConfiguration(@Nullable final IpConfiguration ipConfig) { 108 mBuilderIpConfig = ipConfig == null ? null : new IpConfiguration(ipConfig); 109 return this; 110 } 111 112 /** 113 * Set the {@link NetworkCapabilities} to be used with the {@code Builder}. 114 * @param nc the {@link NetworkCapabilities} to set. 115 * @return The builder to facilitate chaining. 116 */ 117 @NonNull setNetworkCapabilities(@ullable final NetworkCapabilities nc)118 public Builder setNetworkCapabilities(@Nullable final NetworkCapabilities nc) { 119 mBuilderNetworkCapabilities = nc == null ? null : new NetworkCapabilities(nc); 120 return this; 121 } 122 123 /** 124 * Build {@link EthernetNetworkUpdateRequest} return the current update request. 125 * 126 * @throws IllegalStateException when both mBuilderNetworkCapabilities and mBuilderIpConfig 127 * are null. 128 */ 129 @NonNull build()130 public EthernetNetworkUpdateRequest build() { 131 if (mBuilderIpConfig == null && mBuilderNetworkCapabilities == null) { 132 throw new IllegalStateException( 133 "Cannot construct an empty EthernetNetworkUpdateRequest"); 134 } 135 return new EthernetNetworkUpdateRequest(mBuilderIpConfig, mBuilderNetworkCapabilities); 136 } 137 } 138 139 @Override toString()140 public String toString() { 141 return "EthernetNetworkUpdateRequest{" 142 + "mIpConfig=" + mIpConfig 143 + ", mNetworkCapabilities=" + mNetworkCapabilities + '}'; 144 } 145 146 @Override equals(Object o)147 public boolean equals(Object o) { 148 if (this == o) return true; 149 if (o == null || getClass() != o.getClass()) return false; 150 EthernetNetworkUpdateRequest that = (EthernetNetworkUpdateRequest) o; 151 152 return Objects.equals(that.getIpConfiguration(), mIpConfig) 153 && Objects.equals(that.getNetworkCapabilities(), mNetworkCapabilities); 154 } 155 156 @Override hashCode()157 public int hashCode() { 158 return Objects.hash(mIpConfig, mNetworkCapabilities); 159 } 160 161 @Override writeToParcel(@onNull Parcel dest, int flags)162 public void writeToParcel(@NonNull Parcel dest, int flags) { 163 dest.writeParcelable(mIpConfig, flags); 164 dest.writeParcelable(mNetworkCapabilities, flags); 165 } 166 167 @Override describeContents()168 public int describeContents() { 169 return 0; 170 } 171 172 @NonNull 173 public static final Parcelable.Creator<EthernetNetworkUpdateRequest> CREATOR = 174 new Parcelable.Creator<EthernetNetworkUpdateRequest>() { 175 @Override 176 public EthernetNetworkUpdateRequest[] newArray(int size) { 177 return new EthernetNetworkUpdateRequest[size]; 178 } 179 180 @Override 181 public EthernetNetworkUpdateRequest createFromParcel(@NonNull Parcel source) { 182 return new EthernetNetworkUpdateRequest(source); 183 } 184 }; 185 } 186